Skip to content

Commit dd0c8ff

Browse files
committed
feat: updated typescript implementation by Yemi
1 parent 7e06bfc commit dd0c8ff

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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-
let result = 0;
7-
const strLen: number = s.length;
8-
for (let i = 0; i < strLen; i++) {
9-
const ch: string = s.charAt(i);
10-
result = result + Math.abs(s.indexOf(ch) - t.indexOf(ch));
11-
}
12-
return result;
2+
const indexMap = new Map<string, number>();
3+
4+
for (let i = 0; i < t.length; i++) {
5+
indexMap.set(t.charAt(i), i);
6+
}
7+
8+
let result = 0;
9+
for (let i = 0; i < s.length; i++) {
10+
result += Math.abs(i - (indexMap.get(s.charAt(i)) ?? -1));
11+
}
12+
13+
return result;
1314
}

0 commit comments

Comments
 (0)