We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7e06bfc commit dd0c8ffCopy full SHA for dd0c8ff
lesson_13/maps_ts/src/lesson13.ts
@@ -1,13 +1,14 @@
1
-/**
2
- * Provide the solution to LeetCode 3146 here:
3
- * https://leetcode.com/problems/permutation-difference-between-two-strings
4
- */
5
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;
+ const indexMap = new Map<string, number>();
+
+ for (let i = 0; i < t.length; i++) {
+ indexMap.set(t.charAt(i), i);
+ }
+ let result = 0;
+ for (let i = 0; i < s.length; i++) {
+ result += Math.abs(i - (indexMap.get(s.charAt(i)) ?? -1));
13
+ return result;
14
}
0 commit comments