File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change 33 * https://leetcode.com/problems/permutation-difference-between-two-strings
44 */
55export function findPermutationDifference ( s : string , t : string ) : number {
6- return 0 ;
6+ // used chat gpt for this solution implementation
7+
8+ // variable difference keeps track of absolute difference between character index
9+ let difference = 0 ;
10+
11+ // creating a map to store the index of each character in s
12+ const positionMap : Record < string , number > = { } ;
13+ // iterating through the characters in s with i as the index
14+ for ( let i = 0 ; i < s . length ; i ++ ) {
15+ // returns the character at a given index
16+ positionMap [ s [ i ] ] = i ;
17+ }
18+
19+ // calculating permutation difference
20+ for ( let i = 0 ; i < t . length ; i ++ ) {
21+ //using a built in function - math.abs to calculate the difference in character index
22+ // index of character in string s - index of character in string i
23+ difference += Math . abs ( i - positionMap [ t [ i ] ] ) ;
24+ }
25+
26+ return difference ;
727}
28+
You can’t perform that action at this time.
0 commit comments