Skip to content

Commit cb44a2c

Browse files
committed
feat: modifies lesson13.java and lesson 13.ts for decent time complexity and basic hash map utilization to complete the leetcode problem for lesson 13 - Joseph Caballero
1 parent 32f5984 commit cb44a2c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
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+
HashMap<Character, Integer> map = new HashMap<>();
13+
14+
int sum = 0;
15+
for (char letter : s.toCharArray()) {
16+
map.put(letter, s.indexOf(letter));
17+
}
18+
for (int i = 0; i < s.length(); i++) {
19+
int indexOfT = map.get(t.charAt(i));
20+
sum += Math.abs(i - indexOfT);
21+
}
22+
return sum;
1123
}
1224
}

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,14 @@
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 map = new Map<string, number>();
7+
let sum = 0;
8+
for (const char of s) {
9+
map.set(char, s.indexOf(char));
10+
}
11+
for (const [key, value] of map) {
12+
const indexOfT = t.indexOf(key);
13+
sum += Math.abs(value - indexOfT);
14+
}
15+
return sum;
716
}

0 commit comments

Comments
 (0)