8000 Add 3146_permutation_difference_between_two_strings · m3xw3ll/LeetCode@5344bb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5344bb9

Browse files
committed
Add 3146_permutation_difference_between_two_strings
1 parent f47dbde commit 5344bb9

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [Permutation Difference between Two Strings](https://leetcode.com/problems/permutation-difference-between-two-strings/description/)
2+
3+
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.
4+
5+
The permutation difference between s and t is defined as the sum of the absolute difference between the index of the occurrence of each character in s and the index of the occurrence of the same character in t.
6+
7+
Return the permutation difference between s and t.
8+
9+
Example 1:
10+
```
11+
Input: s = "abc", t = "bac"
12+
13+
Output: 2
14+
15+
Explanation:
16+
17+
For s = "abc" and t = "bac", the permutation difference of s and t is equal to the sum of:
18+
19+
The absolute difference between the index of the occurrence of "a" in s and the index of the occurrence of "a" in t.
20+
The absolute difference between the index of the occurrence of "b" in s and the index of the occurrence of "b" in t.
21+
The absolute difference between the index of the occurrence of "c" in s and the index of the occurrence of "c" in t.
22+
That is, the permutation difference between s and t is equal to |0 - 1| + |1 - 0| + |2 - 2| = 2.
23+
```
24+
Example 2:
25+
```
26+
Input: s = "abcde", t = "edbac"
27+
28+
Output: 12
29+
30+
Explanation: The permutation difference between s and t is equal to |0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12.
31+
```
32+
Solution
33+
```python
34+
class Solution:
35+
def findPermutationDifference(self, s: str, t: str) -> int:
36+
d = {}
37+
out = 0
38+
for i in range(len(t)):
39+
d[t[i]] = i
40+
41+
for i in range(len(s)):
42+
out += abs(i - d[s[i]])
43+
return out
44+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def find_permutation_difference(s, t):
2+
d = {}
3+
out = 0
4+
for i in range(len(t)):
5+
d[t[i]] = i
6+
7+
for i in range(len(s)):
8+
out += abs(i - d[s[i]])
9+
return out
10+
11+
12+
13+
print(find_permutation_difference(s = "abc", t = "bac"))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ To search for a specific problem please use ```STRG + F``` to search for.
763763
| 3110 | [Score of a String](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3110_score_of_a_string.md)
764764
| 3120 | [Count the Number of Special Characters I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3120_count_the_number_of_special_characters_one.md)
765765
| 3131 | [Find the Integer Added to Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3131_find_the_integer_added_to_array_one.md)
766+
| 3146 | [Permutation Difference between Two Strings](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3146_permutation_difference_between_two_strings.md)
766767
| 3151 | [Special Array I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3151_special_array_one.md)
767768
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3158_find_the_xor_of_numbers_which_appear_twice.md)
768769
| 3162 | [Find the Number of Good Pairs I](https://github.com/m3xw3ll/LeetCode/blob/master/Algorithms/3162_find_the_number_of_good_pairs_one.md)

0 commit comments

Comments
 (0)
0