Skip to content

Commit 526b3bf

Browse files
committed
feat:HashMap method to compare strings/AngelicaC
1 parent 39c8f59 commit 526b3bf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.codedifferently.lesson13;
2+
3+
public class Char {}
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
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+
var charCountS = new HashMap<Character, Integer>();
13+
14+
int totalDifference = 0;
15+
16+
for (int sIndex = 0; sIndex < s.length(); sIndex++) {
17+
charCountS.put(s.charAt(sIndex), sIndex);
18+
}
19+
for (int tIndex = 0; tIndex < t.length(); tIndex++) {
20+
int countIndexS = charCountS.put(t.charAt(tIndex), tIndex);
21+
22+
totalDifference += Math.abs(countIndexS - tIndex);
23+
}
24+
return totalDifference;
1125
}
1226
}

0 commit comments

Comments
 (0)