File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 1
1
package com .codedifferently .lesson13 ;
2
2
3
+ import java .util .HashMap ;
4
+
3
5
public class Lesson13 {
4
6
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
+
5
12
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 ));
10
16
}
11
17
return result ;
12
- }
18
+ }
13
19
}
You can’t perform that action at this time.
0 commit comments