File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed 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
+ //Create a way to find the index of each character in string s.
7
+ // This could be a list or a dictionary where each character points to its index in s.
8
+ let charPositionMap = new Map < string , number > ( ) ;
9
+ //Start with a variable to keep track of the total permutation difference. Set it to 0.
10
+ let totalDifference = 0 ;
11
+ //For each character in t, find its index in s using the mapping you created.
12
+ for ( let sIndex = 0 ; sIndex < s . length ; sIndex ++ ) {
13
+ charPositionMap . set ( s . charAt ( sIndex ) , sIndex )
14
+ }
15
+
16
+ for ( let tIndex = 0 ; tIndex < t . length ; tIndex ++ ) {
17
+ //Calculate the absolute difference between the index in s and the current index in t.
18
+ let indexInS = charPositionMap . get ( t . charAt ( tIndex ) ) ;
19
+ //Add the absolute difference you calculated to the total difference.
20
+ totalDifference += Math . abs ( indexInS - tIndex ) ;
21
+ }
22
+ //After going through all characters in t, return the total difference.
23
+ return totalDifference ;
7
24
}
You can’t perform that action at this time.
0 commit comments