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 11package com .codedifferently .lesson13 ;
22
3+ import java .util .HashMap ;
4+
35public class Lesson13 {
46
57 /**
68 * Provide the solution to LeetCode 3146 here:
79 * https://leetcode.com/problems/permutation-difference-between-two-strings
810 */
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+ */
919 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 ;
1132 }
1233}
You can’t perform that action at this time.
0 commit comments