Skip to content

Commit 2d62675

Browse files
committed
feat: adds David implementation of findPermutationDifference
1 parent 50680f2 commit 2d62675

File tree

1 file changed

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

1 file changed

+14
-1
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
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+
13+
HashMap<Character, Integer> indexMap = new HashMap<>();
14+
for (int i = 0; i < s.length(); i++) {
15+
indexMap.put(s.charAt(i), i);
16+
}
17+
int difference = 0;
18+
for (int i = 0; i < t.length(); i++) {
19+
if (indexMap.containsKey(t.charAt(i))) {
20+
difference += Math.abs(indexMap.get(t.charAt(i)) - i);
21+
}
22+
}
23+
return difference;
1124
}
1225
}

0 commit comments

Comments
 (0)