Skip to content

Commit 1a3108f

Browse files
committed
feat: Adds David Smith Lesson 13 leetcode 3146 Typescript and Java
1 parent 32f5984 commit 1a3108f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package com.codedifferently.lesson13;
2-
2+
import java.util.HashMap;
33
public class Lesson13 {
44

55
/**
66
* Provide the solution to LeetCode 3146 here:
77
* https://leetcode.com/problems/permutation-difference-between-two-strings
88
*/
99
public int findPermutationDifference(String s, String t) {
10-
return 0;
10+
int difference=0;
11+
HashMap<Character, Integer> indexMap=new HashMap<>();
12+
13+
for (int i = 0; i < s.length(); i++) {
14+
indexMap.put(s.charAt(i), i);
15+
}
16+
17+
for (int i = 0; i < t.length(); i++) {
18+
char ch = t.charAt(i);
19+
int indexInS = indexMap.get(ch);
20+
difference += Math.abs(indexInS - i);
21+
}
22+
23+
return difference;
1124
}
1225
}

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,18 @@
33
* https://leetcode.com/problems/permutation-difference-between-two-strings
44
*/
55
export function findPermutationDifference(s: string, t: string): number {
6-
return 0;
6+
let difference = 0;
7+
const indexMap = new Map<string, number>();
8+
9+
for (let i = 0; i < s.length; i++) {
10+
indexMap.set(s[i], i);
11+
}
12+
13+
for (let i = 0; i < t.length; i++) {
14+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15+
const indexInS = indexMap.get(t[i])!;
16+
difference += Math.abs(indexInS - i);
17+
}
18+
19+
return difference;
720
}

0 commit comments

Comments
 (0)