Skip to content

Commit 19110f6

Browse files
author
AmiyahJo
committed
feat: adds solution to leetCode
comments made to explain certain lines
1 parent a131bdc commit 19110f6

File tree

1 file changed

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

1 file changed

+19
-1
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
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 charIndexMap = new HashMap<Character, Integer>();
13+
14+
int totalDifference = 0;
15+
16+
for (int sIndex = 0; sIndex < s.length(); sIndex++) {
17+
charIndexMap.put(s.charAt(sIndex), sIndex);
18+
//keeps track of the position for each character in 's'
19+
}
20+
21+
for (int tIndex = 0; tIndex < t.length(); tIndex++) {
22+
int indexInS = charIndexMap.get(t.charAt(tIndex));
23+
//Same as sIndex , but this loop keeps track of each character in t
24+
totalDifference += Math.abs(indexInS - tIndex);
25+
//adds all the differences
26+
}
27+
28+
return totalDifference;
1129
}
1230
}

0 commit comments

Comments
 (0)