Class Solution
-
- All Implemented Interfaces:
public final class Solution3146 - Permutation Difference between Two Strings.
Easy
You are given two strings
sandtsuch that every character occurs at most once insandtis a permutation ofs.The permutation difference between
sandtis defined as the sum of the absolute difference between the index of the occurrence of each character insand the index of the occurrence of the same character int.Return the permutation difference between
sandt.Example 1:
Input: s = "abc", t = "bac"
Output: 2
Explanation:
For
s = "abc"andt = "bac", the permutation difference ofsandtis equal to the sum of:The absolute difference between the index of the occurrence of
"a"insand the index of the occurrence of"a"int.The absolute difference between the index of the occurrence of
"b"insand the index of the occurrence of"b"int.The absolute difference between the index of the occurrence of
"c"insand the index of the occurrence of"c"int.
That is, the permutation difference between
sandtis equal to|0 - 1| + |2 - 2| + |1 - 0| = 2.Example 2:
Input: s = "abcde", t = "edbac"
Output: 12
Explanation: The permutation difference between
sandtis equal to|0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12.Constraints:
1 <= s.length <= 26Each character occurs at most once in
s.tis a permutation ofs.sconsists only of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerfindPermutationDifference(String s, String t)-
-
Method Detail
-
findPermutationDifference
final Integer findPermutationDifference(String s, String t)
-
-
-
-