Skip to content

Commit 7c12ebb

Browse files
committed
feat: implementation of lesson13 using HashMap by Yemi
1 parent f4d679c commit 7c12ebb

File tree

1 file changed

+11
-5
lines changed
  • lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13

1 file changed

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

3+
import java.util.HashMap;
4+
35
public class Lesson13 {
46
public int findPermutationDifference(String s, String t) {
7+
var indexMap = new HashMap<Character, Integer>();
8+
for (int i = 0; i < t.length(); i++) {
9+
indexMap.put(t.charAt(i), i);
10+
}
11+
512
int result = 0;
6-
int strLen = s.length();
7-
for (int i = 0; i < strLen; i++) {
8-
char ch = s.charAt(i);
9-
result = result + (Math.abs(s.indexOf(ch) - t.indexOf(ch)));
13+
for (int i = 0; i < s.length(); i++) {
14+
// Default to -1 if char not found
15+
result += Math.abs(i - indexMap.getOrDefault(s.charAt(i), -1));
1016
}
1117
return result;
12-
}
18+
}
1319
}

0 commit comments

Comments
 (0)