File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +24
-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
*/
9
11
public int findPermutationDifference (String s , String t ) {
10
- return 0 ;
12
+ HashMap <Character , Integer > indexMap = new HashMap <>();
13
+
14
+ for (int i =0 ; i < s .length (); i ++) {
15
+ indexMap .put (s .charAt (i ), i );
16
+ }
17
+
18
+ HashMap <Character , Integer > indexDifferences = new HashMap <>();
19
+ int sumOfAbsoluteDiff = 0 ;
20
+
21
+ for (int i = 0 ; i < t .length (); i ++) {
22
+ char c = t .charAt (i );
23
+ int sIndex = indexMap .get (c );
24
+ int tIndex = i ;
25
+
26
+ int indexDiff = Math .abs (sIndex - tIndex );
27
+
28
+ indexDifferences .put (c , indexDiff );
29
+
30
+ sumOfAbsoluteDiff += indexDiff ;
31
+ }
32
+
33
+ return sumOfAbsoluteDiff ;
11
34
}
12
35
}
You can’t perform that action at this time.
0 commit comments