File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +22
-1
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
/**
6
8
* Provide the solution to LeetCode 3146 here:
7
9
* https://leetcode.com/problems/permutation-difference-between-two-strings
8
10
*/
11
+
12
+ /**
13
+ * We need to return the absolute difference btw. a chars index in both 's' and 't'
14
+ * if s = "abc" and t = "bac"
15
+ * for s: a @index 0, b @index 1, c @index 2
16
+ * for t: a @index 1, b @index 0, c @index 2
17
+ * the math: |0 - 1| + |1 - 0| + \2 - 2| = |-1| + |1| + |0| = 1 + 1 + 0 = 2 we return 2
18
+ */
9
19
public int findPermutationDifference (String s , String t ) {
10
- return 0 ;
20
+ var compareStrings = new HashMap <Character , Integer >();
21
+ int total = 0 ;
22
+
23
+ for (int i = 0 ; i < s .length (); i ++) {
24
+ compareStrings .put (s .charAt (i ), s .indexOf (s .charAt (i )));
25
+ }
26
+
27
+ for (int i = 0 ; i < t .length (); i ++) {
28
+ int absDiff = Math .abs (compareStrings .get (s .charAt (i )) - t .indexOf (s .charAt (i )));
29
+ total += absDiff ;
30
+ }
31
+ return total ;
11
32
}
12
33
}
You can’t perform that action at this time.
0 commit comments