Skip to content

Commit e95e3ea

Browse files
feat: implemented solution to LeetCode 3146 using a hashmap
1 parent 39c8f59 commit e95e3ea

File tree

1 file changed

+15
-1
lines changed
  • lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13

1 file changed

+15
-1
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
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+
for(int i = 0; i<t.length(); i++){
15+
map.put(t.charAt(i), i);
16+
}
17+
18+
int difference = 0;
19+
for(int i = 0; i<s.length(); i++) {
20+
int idx = map.get(s.charAt(i));
21+
difference += Math.abs(i - idx);
22+
}
23+
24+
return difference;
1125
}
1226
}

0 commit comments

Comments
 (0)