Skip to content

Commit 5fef93b

Browse files
committed
feat: implement findPermutationDifference method in Lesson13 class
1 parent c9f5fa1 commit 5fef93b

File tree

1 file changed

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

1 file changed

+24
-1
lines changed
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+
var charByIndex = new HashMap<Character, Integer>();
13+
14+
// Stores each character index from s into an Array
15+
for (int i = 0; i < s.length(); ++i) {
16+
charByIndex.put(s.charAt(i), i);
17+
}
18+
19+
// will be the difference between s and t's index's
20+
int difference = 0;
21+
22+
for (int j = 0; j < t.length(); ++j) {
23+
if (charByIndex.containsKey(t.charAt(j))) {
24+
// Math.abs gets the absole number ensuring the difference is always positive
25+
// My logic here is that it will find the index of t and subtract that from j which is that
26+
// original index of that char
27+
difference += Math.abs(charByIndex.get(t.charAt(j)) - j);
28+
}
29+
}
30+
// Runs a for loop that will be the length of t but this time we will be using the map t
31+
// tell our loop to run until it finds the same character
32+
System.out.println(charByIndex);
33+
return difference;
1134
}
1235
}

0 commit comments

Comments
 (0)