Skip to content

Commit a44ec7d

Browse files
committed
feat: implement findPermutationDifference function in TypeScript and remove debug print statement in Java
1 parent 5fef93b commit a44ec7d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public int findPermutationDifference(String s, String t) {
2929
}
3030
// Runs a for loop that will be the length of t but this time we will be using the map t
3131
// tell our loop to run until it finds the same character
32-
System.out.println(charByIndex);
3332
return difference;
3433
}
3534
}

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,19 @@
33
* https://leetcode.com/problems/permutation-difference-between-two-strings
44
*/
55
export function findPermutationDifference(s: string, t: string): number {
6-
return 0;
6+
const charByIndex = new Map<String, number>();
7+
8+
for(let i = 0; i < s.length; ++i) {
9+
charByIndex.set(s[i], i);
10+
}
11+
12+
let difference = 0;
13+
14+
for (let j = 0; j < t.length; ++j) {
15+
if(charByIndex.has(t[j])) {
16+
//as number is telling the computer to expect a number
17+
difference += Math.abs((charByIndex.get(t[j]) as number) - j);
18+
}
19+
}
20+
return difference;
721
}

0 commit comments

Comments
 (0)