Skip to content

Commit 4ad13ed

Browse files
committed
HashMap Update: Lesson 13 by Dwight Blue
1 parent 1b08d0f commit 4ad13ed

File tree

1 file changed

+9
-7
lines changed
  • lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13

1 file changed

+9
-7
lines changed

lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.codedifferently.lesson13;
22

3+
import java.util.HashMap;
4+
35
public class Lesson13 {
46

57
/**
@@ -8,15 +10,15 @@ public class Lesson13 {
810
*/
911
public int findPermutationDifference(String s, String t) {
1012
var sum = 0;
11-
char[] charArray1 = s.toCharArray();
12-
char[] charArray2 = t.toCharArray();
13+
HashMap<Character, Integer> strStone = new HashMap<>();
1314

1415
for (int i = 0; i < s.length(); i++) {
15-
for (int j = 0; j < t.length(); j++) {
16-
if (charArray1[i] == charArray2[j]) {
17-
sum = sum + Math.abs(i - j);
18-
}
19-
}
16+
strStone.put(s.charAt(i), i);
17+
}
18+
for (char key : strStone.keySet()) {
19+
int indexSTR = t.indexOf(Character.toString(key));
20+
21+
sum += Math.abs(s.indexOf(key) - indexSTR);
2022
}
2123
return sum;
2224
}

0 commit comments

Comments
 (0)