File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +24
-1
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+ var charByIndex = new HashMap <Character , Integer >();
13+
14+ // Stores each character index from s into an Array
15+ for (int i = 0 ; i < s .length (); ++i ) {
16+ charByIndex .put (s .charAt (i ), i );
17+ }
18+
19+ // will be the difference between s and t's index's
20+ int difference = 0 ;
21+
22+ for (int j = 0 ; j < t .length (); ++j ) {
23+ if (charByIndex .containsKey (t .charAt (j ))) {
24+ // Math.abs gets the absole number ensuring the difference is always positive
25+ // My logic here is that it will find the index of t and subtract that from j which is that
26+ // original index of that char
27+ difference += Math .abs (charByIndex .get (t .charAt (j )) - j );
28+ }
29+ }
30+ // Runs a for loop that will be the length of t but this time we will be using the map t
31+ // tell our loop to run until it finds the same character
32+ System .out .println (charByIndex );
33+ return difference ;
1134 }
1235}
You can’t perform that action at this time.
0 commit comments