File tree Expand file tree Collapse file tree 2 files changed +29
-3
lines changed
maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Original file line number Diff line number Diff line change 11package com .codedifferently .lesson13 ;
2-
2+ import java . util . HashMap ;
33public class Lesson13 {
44
55 /**
66 * Provide the solution to LeetCode 3146 here:
77 * https://leetcode.com/problems/permutation-difference-between-two-strings
88 */
99 public int findPermutationDifference (String s , String t ) {
10- return 0 ;
10+ int difference =0 ;
11+ HashMap <Character , Integer > indexMap =new HashMap <>();
12+
13+ for (int i = 0 ; i < s .length (); i ++) {
14+ indexMap .put (s .charAt (i ), i );
15+ }
16+
17+ for (int i = 0 ; i < t .length (); i ++) {
18+ char ch = t .charAt (i );
19+ int indexInS = indexMap .get (ch );
20+ difference += Math .abs (indexInS - i );
21+ }
22+
23+ return difference ;
1124 }
1225}
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+ let difference = 0 ;
7+ const indexMap = new Map < string , number > ( ) ;
8+
9+ for ( let i = 0 ; i < s . length ; i ++ ) {
10+ indexMap . set ( s [ i ] , i ) ;
11+ }
12+
13+ for ( let i = 0 ; i < t . length ; i ++ ) {
14+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
15+ const indexInS = indexMap . get ( t [ i ] ) ! ;
16+ difference += Math . abs ( indexInS - i ) ;
17+ }
18+
19+ return difference ;
720}
You can’t perform that action at this time.
0 commit comments