Skip to content

Commit 16fc235

Browse files
implemented findPermutationDifference function
1 parent abb394d commit 16fc235

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
/**
2-
* Provide the solution to LeetCode 3146 here:
3-
* https://leetcode.com/problems/permutation-difference-between-two-strings
4-
*/
51
export function findPermutationDifference(s: string, t: string): number {
6-
return 0;
2+
const tIndexMap = new Map<string, number>();
3+
4+
for (let i = 0; i < t.length; i++) {
5+
tIndexMap.set(t[i], i);
6+
}
7+
8+
let totalDifference = 0;
9+
10+
for (let i = 0; i < s.length; i++) {
11+
const c = s[i];
12+
const indexInT = tIndexMap.get(c);
13+
14+
if (indexInT !== undefined) {
15+
totalDifference += Math.abs(i - indexInT);
16+
}
17+
}
18+
19+
return totalDifference;
720
}

0 commit comments

Comments
 (0)