Skip to content

Commit a1637c4

Browse files
author
jjcapparell
committed
Feat: added content to lesson13.ts
1 parent 29cc80c commit a1637c4

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
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 stringS: Map<string, number> = new Map();
3+
for (let i = 0; i < s.length; i++) {
4+
stringS.set(s.charAt(i), i);
5+
}
6+
7+
const stringT: Map<string, number> = new Map();
8+
for (let i = 0; i < t.length; i++) {
9+
stringT.set(t.charAt(i), i);
10+
}
11+
12+
return compareMaps(stringS, stringT);
713
}
14+
15+
function compareMaps(map1: Map<string, number>, map2: Map<string, number>): number {
16+
let diffSum = 0;
17+
for (const key of map1.keys()) {
18+
if (map2.has(key)) {
19+
const value1 = map1.get(key)!; // Non-null assertion because we checked with has()
20+
const value2 = map2.get(key)!; // Non-null assertion
21+
const difference = Math.abs(value1 - value2);
22+
diffSum += difference;
23+
}
24+
}
25+
return diffSum;
26+
}

0 commit comments

Comments
 (0)