Skip to content

Commit e610370

Browse files
feat: adds lesson 13 permutation difference solution in typescript
1 parent c490464 commit e610370

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,26 @@
33
* https://leetcode.com/problems/permutation-difference-between-two-strings
44
*/
55
export function findPermutationDifference(s: string, t: string): number {
6-
return 0;
6+
// used chat gpt for this solution implementation
7+
8+
// variable difference keeps track of absolute difference between character index
9+
let difference = 0;
10+
11+
// creating a map to store the index of each character in s
12+
const positionMap: Record<string, number> = {};
13+
// iterating through the characters in s with i as the index
14+
for (let i = 0; i < s.length; i++) {
15+
// returns the character at a given index
16+
positionMap[s[i]] = i;
17+
}
18+
19+
// calculating permutation difference
20+
for (let i = 0; i < t.length; i++) {
21+
//using a built in function - math.abs to calculate the difference in character index
22+
// index of character in string s - index of character in string i
23+
difference += Math.abs(i - positionMap[t[i]]);
24+
}
25+
26+
return difference;
727
}
28+

0 commit comments

Comments
 (0)