File tree Expand file tree Collapse file tree 1 file changed +24
-5
lines changed Expand file tree Collapse file tree 1 file changed +24
-5
lines changed Original file line number Diff line number Diff line change 1
- /**
2
- * Provide the solution to LeetCode 3146 here:
3
- * https://leetcode.com/problems/permutation-difference-between-two-strings
4
- */
5
1
export function findPermutationDifference ( s : string , t : string ) : number {
6
- return 0 ;
2
+ const stringS : Map < string , number > = new Map ( ) ;
3
+ for ( let i = 0 ; i < s . length ; i ++ ) {
4
+ stringS . set ( s . charAt ( i ) , i ) ;
5
+ }
6
+
7
+ const stringT : Map < string , number > = new Map ( ) ;
8
+ for ( let i = 0 ; i < t . length ; i ++ ) {
9
+ stringT . set ( t . charAt ( i ) , i ) ;
10
+ }
11
+
12
+ return compareMaps ( stringS , stringT ) ;
7
13
}
14
+
15
+ function compareMaps ( map1 : Map < string , number > , map2 : Map < string , number > ) : number {
16
+ let diffSum = 0 ;
17
+ for ( const key of map1 . keys ( ) ) {
18
+ if ( map2 . has ( key ) ) {
19
+ const value1 = map1 . get ( key ) ! ; // Non-null assertion because we checked with has()
20
+ const value2 = map2 . get ( key ) ! ; // Non-null assertion
21
+ const difference = Math . abs ( value1 - value2 ) ;
22
+ diffSum += difference ;
23
+ }
24
+ }
25
+ return diffSum ;
26
+ }
You can’t perform that action at this time.
0 commit comments