File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed
maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 2 files changed +25
-2
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 > ABC = new HashMap <>();
13
+ int permDiff = 0 ;
14
+ for (int i = 0 ; i < s .length (); i ++) {
15
+ ABC .put (s .charAt (i ), i );
16
+ }
17
+
18
+ for (char key : ABC .keySet ()) {
19
+ int indexInT = t .indexOf (Character .toString (key ));
20
+
21
+ permDiff += Math .abs (s .indexOf (key ) - indexInT );
22
+ }
23
+ return permDiff ;
11
24
}
12
25
}
Original file line number Diff line number Diff line change 3
3
* https://leetcode.com/problems/permutation-difference-between-two-strings
4
4
*/
5
5
export function findPermutationDifference ( s : string , t : string ) : number {
6
- return 0 ;
6
+ const ABC = new Map < string , number > ( ) ;
7
+ let permDiff = 0 ;
8
+ for ( let i = 0 ; i < s . length ; i ++ ) {
9
+ ABC . set ( s . charAt ( i ) , i ) ;
10
+ }
11
+
12
+ for ( const key of ABC . keys ( ) ) {
13
+ const indexInT = t . indexOf ( key ) ;
14
+ permDiff += Math . abs ( s . indexOf ( key ) - indexInT ) ;
15
+ }
16
+ return permDiff ;
7
17
}
You can’t perform that action at this time.
0 commit comments