Skip to content

Commit 3499a6b

Browse files
committed
feat: generated logic for findPermutationDifference method using HashMap
1 parent 32f5984 commit 3499a6b

File tree

1 file changed

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

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
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
*/
11+
12+
/**
13+
* We need to return the absolute difference btw. a chars index in both 's' and 't'
14+
* if s = "abc" and t = "bac"
15+
* for s: a @index 0, b @index 1, c @index 2
16+
* for t: a @index 1, b @index 0, c @index 2
17+
* the math: |0 - 1| + |1 - 0| + \2 - 2| = |-1| + |1| + |0| = 1 + 1 + 0 = 2 we return 2
18+
*/
919
public int findPermutationDifference(String s, String t) {
10-
return 0;
20+
var compareStrings = new HashMap<Character, Integer>();
21+
int total = 0;
22+
23+
for (int i = 0; i < s.length(); i++) {
24+
compareStrings.put(s.charAt(i), s.indexOf(s.charAt(i)));
25+
}
26+
27+
for (int i = 0; i < t.length(); i++) {
28+
int absDiff = Math.abs(compareStrings.get(s.charAt(i)) - t.indexOf(s.charAt(i)));
29+
total += absDiff;
30+
}
31+
return total;
1132
}
1233
}

0 commit comments

Comments
 (0)