Skip to content

Commit 09fa451

Browse files
author
“A1-4U2T1NN”
committed
feat: created string permutation distance calculator; added comments explaining each line of code;
1 parent bf252ad commit 09fa451

File tree

1 file changed

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

1 file changed

+17
-1
lines changed

lesson_13/maps_java/maps_app/src/main/java/com/codedifferently/lesson13/Lesson13.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,24 @@ public class Lesson13 {
55
/**
66
* Provide the solution to LeetCode 3146 here:
77
* https://leetcode.com/problems/permutation-difference-between-two-strings
8+
* You are given two strings s and t such that every character occurs at most once in s and t is a permutation of s.
9+
* The permutation difference between s and t is defined as
10+
* the sum of the absolute difference between the index of the occurrence
11+
* of each character in s and the index of the occurrence of the same character in t.
12+
* Return the permutation difference between s and t.
813
*/
914
public int findPermutationDifference(String s, String t) {
10-
return 0;
15+
16+
int permuDifference = 0;
17+
// Created a counter to track Permutation Difference
18+
for (int i = 0; i < s.length(); i++) {
19+
// Loops through each letter in string 's'
20+
int difference = s.indexOf(s.charAt(i)) - t.indexOf(s.charAt(i));
21+
// Subtracts possitional value of string 't' from the possitional value of string 's'
22+
permuDifference += Math.abs(difference);
23+
// Converts difference into absolute value and adds it to the Permutation Difference
24+
}
25+
return permuDifference;
26+
// Returns the total Permutation Difference value
1127
}
1228
}

0 commit comments

Comments
 (0)