File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed
lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13 Expand file tree Collapse file tree 1 file changed +27
-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+ HashMap <Character , Integer > indexMap = new HashMap <> ();
13+ for (int i =0 ; i < s .length (); i ++){
14+ indexMap .put (s .charAt (i ), i );
15+ }
16+ int difference =0 ;
17+
18+ for (int i =0 ; i < t .length (); i ++) {
19+ char ch = t .charAt (i );
20+ int originalIndex = indexMap .get (ch );
21+ difference += Math .abs (originalIndex -i );
22+
23+ }
24+ return difference ;
25+ }
26+ //used chatgpt because I needed help creating an instance before calling
27+ public static void main (String [] args ) {
28+ // Create an object of Lesson13 to call the method
29+ Lesson13 obj = new Lesson13 ();
30+ System .out .println (obj .findPermutationDifference ("abc" , "bac" )); // Output: 2
31+ System .out .println (obj .findPermutationDifference ("abcde" , "edbac" )); // Output: 12
1132 }
1233}
34+ //Constraints:
35+ //1 <= s.length <= 26
36+ //Each character occurs at most once in s.
37+ //t is a permutation of s.
38+ //s consists only of lowercase English letters.
You can’t perform that action at this time.
0 commit comments