File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 11package com .codedifferently .lesson13 ;
22
3+ import java .util .HashMap ;
4+
35public class Lesson13 {
46
57 /**
68 * Provide the solution to LeetCode 3146 here:
79 * https://leetcode.com/problems/permutation-difference-between-two-strings
810 */
911 public int findPermutationDifference (String s , String t ) {
10- return 0 ;
12+ // initializing the permuation difference between the characters
13+ int difference = 0 ;
14+
15+ // creating a hashmap to store the index of each character in s - the key value pair
16+ // the key is the character and the value is the index
17+ HashMap <Character , Integer > characterPosition = new HashMap <>();
18+
19+ // iterates through character in s with i as index
20+ for (int i = 0 ; i < s .length (); i ++) {
21+ // returns the map of character at a given index
22+ characterPosition .put (s .charAt (i ), i );
23+ }
24+
25+ // calcualting permutation difference
26+ for (int i = 0 ; i < t .length (); i ++) {
27+ //using a built in function - math.abs to calculate the difference in character index
28+ // index of character in string s - index of character in string i
29+ difference += Math .abs (i - characterPosition .get (t .charAt (i )));
30+ }
31+ return difference ;
1132 }
1233}
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- // used chat gpt for this solution implementation
6+ // used chat gpt for this extra credit solution implementation
77
88 // variable difference keeps track of absolute difference between character index
99 let difference = 0 ;
You can’t perform that action at this time.
0 commit comments