Skip to content

Commit a43aec6

Browse files
feat: adds lesson 13 permutation difference solution in javascript and java
1 parent e610370 commit a43aec6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
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+
// 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
}

lesson_13/maps_ts/src/lesson13.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* https://leetcode.com/problems/permutation-difference-between-two-strings
44
*/
55
export 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;

0 commit comments

Comments
 (0)