Skip to content

Commit 3ce466e

Browse files
committed
lesson_13_homework
1 parent abb394d commit 3ce466e

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

lesson_13/maps_java/maps_app/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
package com.codedifferently.lesson13;
22

3+
import java.util.HashMap;
4+
35
public class Lesson13 {
46

57
/**
68
* Provide the solution to LeetCode 3146 here:
79
* https://leetcode.com/problems/permutation-difference-between-two-strings
810
*/
911
public int findPermutationDifference(String s, String t) {
10-
return 0;
12+
int difference = 0;
13+
14+
var charByIndex = new HashMap<Character, Integer>();
15+
16+
for (int i = 0; i < s.length(); i++) {
17+
charByIndex.put(s.charAt(i), i);
18+
}
19+
20+
for (int j = 0; j < t.length(); j++) {
21+
char ch = t.charAt(j);
22+
23+
if (charByIndex.containsKey(ch)) {
24+
25+
difference += Math.abs(charByIndex.get(ch) - j);
26+
} else {
27+
28+
int indexInS = s.indexOf(ch);
29+
difference += Math.abs(indexInS - j);
30+
}
31+
}
32+
33+
return difference;
1134
}
1235
}

lesson_13/maps_java/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
const charByIndex = new Map<string, number>();
7+
let difference = 0;
8+
9+
for (let i = 0; i < s.length; ++i) {
10+
charByIndex.set(s[i], i);
11+
}
12+
13+
for (let j = 0; j < t.length; ++j) {
14+
if (charByIndex.has(t[j])) {
15+
16+
difference += Math.abs(charByIndex.get(t[j]) as number - j);
17+
}
18+
}
19+
return difference;
720
}

0 commit comments

Comments
 (0)