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 11package com .codedifferently .lesson13 ;
22
3+ import java .util .HashMap ;
4+
35public 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 }
You can’t perform that action at this time.
0 commit comments