Skip to content

Commit c09a350

Browse files
author
AmiyahJo
committed
feat: adds typescript implementation in lesson13.ts
1 parent 19110f6 commit c09a350

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,22 @@
33
* https://leetcode.com/problems/permutation-difference-between-two-strings
44
*/
55
export function findPermutationDifference(s: string, t: string): number {
6-
return 0;
6+
//Create a way to find the index of each character in string s.
7+
// This could be a list or a dictionary where each character points to its index in s.
8+
let charPositionMap = new Map<string, number>();
9+
//Start with a variable to keep track of the total permutation difference. Set it to 0.
10+
let totalDifference = 0;
11+
//For each character in t, find its index in s using the mapping you created.
12+
for (let sIndex = 0; sIndex < s.length; sIndex++) {
13+
charPositionMap.set(s.charAt(sIndex), sIndex)
14+
}
15+
16+
for (let tIndex = 0; tIndex < t.length; tIndex++) {
17+
//Calculate the absolute difference between the index in s and the current index in t.
18+
let indexInS = charPositionMap.get(t.charAt(tIndex));
19+
//Add the absolute difference you calculated to the total difference.
20+
totalDifference += Math.abs(indexInS - tIndex);
21+
}
22+
//After going through all characters in t, return the total difference.
23+
return totalDifference;
724
}

0 commit comments

Comments
 (0)