File tree Expand file tree Collapse file tree 1 file changed +6
-14
lines changed Expand file tree Collapse file tree 1 file changed +6
-14
lines changed Original file line number Diff line number Diff line change 1
1
export function findPermutationDifference ( s : string , t : string ) : number {
2
2
const stringS = new Map < string , number > ( ) ;
3
- for ( let i = 0 ; i < s . length ; i ++ ) {
4
- stringS . set ( s . charAt ( i ) , i ) ;
5
- }
6
-
7
3
const stringT = new Map < string , number > ( ) ;
4
+ let diffSum = 0 ;
8
5
for ( let i = 0 ; i < t . length ; i ++ ) {
6
+ stringS . set ( s . charAt ( i ) , i ) ;
9
7
stringT . set ( t . charAt ( i ) , i ) ;
10
8
}
11
-
12
- return compareMaps ( stringS , stringT ) ;
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 ) ) {
9
+ for ( const key of stringS . keys ( ) ) {
10
+ if ( stringT . has ( key ) ) {
19
11
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
20
- const value1 = map1 . get ( key ) ! ; // Non-null assertion because we checked with has()
12
+ const value1 = stringS . get ( key ) ! ; // Non-null assertion because we checked with has()
21
13
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22
- const value2 = map2 . get ( key ) ! ; // Non-null assertion
14
+ const value2 = stringT . get ( key ) ! ; // Non-null assertion
23
15
const difference = Math . abs ( value1 - value2 ) ;
24
16
diffSum += difference ;
25
17
}
You can’t perform that action at this time.
0 commit comments