Skip to content

Commit f476c53

Browse files
committed
Feat: lesson 13 leet code java solution
1 parent abb394d commit f476c53

File tree

1 file changed

+27
-1
lines changed
  • lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13

1 file changed

+27
-1
lines changed
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
package com.codedifferently.lesson13;
22

3+
import java.util.HashMap;
4+
35
public 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.

0 commit comments

Comments
 (0)