File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +9
-7
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
5
7
/**
@@ -8,15 +10,15 @@ public class Lesson13 {
8
10
*/
9
11
public int findPermutationDifference (String s , String t ) {
10
12
var sum = 0 ;
11
- char [] charArray1 = s .toCharArray ();
12
- char [] charArray2 = t .toCharArray ();
13
+ HashMap <Character , Integer > strStone = new HashMap <>();
13
14
14
15
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 );
20
22
}
21
23
return sum ;
22
24
}
You can’t perform that action at this time.
0 commit comments