Skip to content

Commit d7f3f61

Browse files
committed
+ problem 1202
1 parent bead5e0 commit d7f3f61

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1202. Smallest String With Swaps
2+
You are given a string `s`, and an array of pairs of indices in the string `pairs` where `pairs[i] = [a, b]` indicates 2 indices(0-indexed) of the string.
3+
4+
You can swap the characters at any pair of indices in the given `pairs` **any number of times**.
5+
6+
Return the lexicographically smallest string that `s` can be changed to after using the swaps.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2]]
11+
<strong>Output:</strong> "bacd"
12+
<strong>Explanation:</strong>
13+
Swap s[0] and s[3], s = "bcad"
14+
Swap s[1] and s[2], s = "bacd"
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
20+
<strong>Output:</strong> "abcd"
21+
<strong>Explanation:</strong>
22+
Swap s[0] and s[3], s = "bcad"
23+
Swap s[0] and s[2], s = "acbd"
24+
Swap s[1] and s[2], s = "abcd"
25+
</pre>
26+
27+
#### Example 3:
28+
<pre>
29+
<strong>Input:</strong> s = "cba", pairs = [[0,1],[1,2]]
30+
<strong>Output:</strong> "abc"
31+
<strong>Explanation:</strong>
32+
Swap s[0] and s[1], s = "bca"
33+
Swap s[1] and s[2], s = "bac"
34+
Swap s[0] and s[1], s = "abc"
35+
</pre>
36+
37+
#### Constraints:
38+
* `1 <= s.length <= 10^5`
39+
* `0 <= pairs.length <= 10^5`
40+
* `0 <= pairs[i][0], pairs[i][1] < s.length`
41+
* `s` only contains lower case English letters.
42+
43+
## Solutions (Python)
44+
45+
### 1. Solution
46+
```Python
47+
class Solution:
48+
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
49+
parent = list(range(len(s)))
50+
groups = {}
51+
chars = []
52+
53+
for a, b in pairs:
54+
while parent[a] != parent[parent[a]]:
55+
parent[a] = parent[parent[a]]
56+
while parent[b] != parent[parent[b]]:
57+
parent[b] = parent[parent[b]]
58+
59+
if parent[a] < parent[b]:
60+
parent[parent[b]] = parent[a]
61+
else:
62+
parent[parent[a]] = parent[b]
63+
64+
for i in range(len(s)):
65+
while parent[i] != parent[parent[i]]:
66+
parent[i] = parent[parent[i]]
67+
68+
if parent[i] not in groups:
69+
groups[parent[i]] = []
70+
groups[parent[i]].append(i)
71+
72+
for group in groups:
73+
groups[group].sort(key=lambda i: -ord(s[i]))
74+
75+
for i in range(len(s)):
76+
chars.append(s[groups[parent[i]].pop()])
77+
78+
return ''.join(chars)
79+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 1202. 交换字符串中的元素
2+
给你一个字符串 `s`,以及该字符串中的一些「索引对」数组 `pairs`,其中 `pairs[i] = [a, b]` 表示字符串中的两个索引(编号从 0 开始)。
3+
4+
你可以 **任意多次交换**`pairs` 中任意一对索引处的字符。
5+
6+
返回在经过若干次交换后,`s` 可以变成的按字典序最小的字符串。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> s = "dcab", pairs = [[0,3],[1,2]]
11+
<strong>输出:</strong> "bacd"
12+
<strong>解释:</strong>
13+
交换 s[0] 和 s[3], s = "bcad"
14+
交换 s[1] 和 s[2], s = "bacd"
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> s = "dcab", pairs = [[0,3],[1,2],[0,2]]
20+
<strong>输出:</strong> "abcd"
21+
<strong>解释:</strong>
22+
交换 s[0] 和 s[3], s = "bcad"
23+
交换 s[0] 和 s[2], s = "acbd"
24+
交换 s[1] 和 s[2], s = "abcd"
25+
</pre>
26+
27+
#### 示例 3:
28+
<pre>
29+
<strong>输入:</strong> s = "cba", pairs = [[0,1],[1,2]]
30+
<strong>输出:</strong> "abc"
31+
<strong>解释:</strong>
32+
交换 s[0] 和 s[1], s = "bca"
33+
交换 s[1] 和 s[2], s = "bac"
34+
交换 s[0] 和 s[1], s = "abc"
35+
</pre>
36+
37+
#### Constraints:
38+
* `1 <= s.length <= 10^5`
39+
* `0 <= pairs.length <= 10^5`
40+
* `0 <= pairs[i][0], pairs[i][1] < s.length`
41+
* `s` 中只含有小写英文字母
42+
43+
## 题解 (Python)
44+
45+
### 1. 题解
46+
```Python
47+
class Solution:
48+
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
49+
parent = list(range(len(s)))
50+
groups = {}
51+
chars = []
52+
53+
for a, b in pairs:
54+
while parent[a] != parent[parent[a]]:
55+
parent[a] = parent[parent[a]]
56+
while parent[b] != parent[parent[b]]:
57+
parent[b] = parent[parent[b]]
58+
59+
if parent[a] < parent[b]:
60+
parent[parent[b]] = parent[a]
61+
else:
62+
parent[parent[a]] = parent[b]
63+
64+
for i in range(len(s)):
65+
while parent[i] != parent[parent[i]]:
66+
parent[i] = parent[parent[i]]
67+
68+
if parent[i] not in groups:
69+
groups[parent[i]] = []
70+
groups[parent[i]].append(i)
71+
72+
for group in groups:
73+
groups[group].sort(key=lambda i: -ord(s[i]))
74+
75+
for i in range(len(s)):
76+
chars.append(s[groups[parent[i]].pop()])
77+
78+
return ''.join(chars)
79+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
3+
parent = list(range(len(s)))
4+
groups = {}
5+
chars = []
6+
7+
for a, b in pairs:
8+
while parent[a] != parent[parent[a]]:
9+
parent[a] = parent[parent[a]]
10+
while parent[b] != parent[parent[b]]:
11+
parent[b] = parent[parent[b]]
12+
13+
if parent[a] < parent[b]:
14+
parent[parent[b]] = parent[a]
15+
else:
16+
parent[parent[a]] = parent[b]
17+
18+
for i in range(len(s)):
19+
while parent[i] != parent[parent[i]]:
20+
parent[i] = parent[parent[i]]
21+
22+
if parent[i] not in groups:
23+
groups[parent[i]] = []
24+
groups[parent[i]].append(i)
25+
26+
for group in groups:
27+
groups[group].sort(key=lambda i: -ord(s[i]))
28+
29+
for i in range(len(s)):
30+
chars.append(s[groups[parent[i]].pop()])
31+
32+
return ''.join(chars)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
[1190][1190l]|[Reverse Substrings Between Each Pair of Parentheses][1190] |![rs]
621621
[1191][1191l]|[K-Concatenation Maximum Sum][1191] |![rb]&nbsp;&nbsp;![rs]
622622
[1200][1200l]|[Minimum Absolute Difference][1200] |![rs]
623+
[1202][1202l]|[Smallest String With Swaps][1202] |![py]
623624
[1207][1207l]|[Unique Number of Occurrences][1207] |![rs]
624625
[1208][1208l]|[Get Equal Substrings Within Budget][1208] |![rb]&nbsp;&nbsp;![rs]
625626
[1209][1209l]|[Remove All Adjacent Duplicates in String II][1209] |![rs]
@@ -1861,6 +1862,7 @@
18611862
[1190]:Problemset/1190-Reverse%20Substrings%20Between%20Each%20Pair%20of%20Parentheses/README.md#1190-reverse-substrings-between-each-pair-of-parentheses
18621863
[1191]:Problemset/1191-K-Concatenation%20Maximum%20Sum/README.md#1191-k-concatenation-maximum-sum
18631864
[1200]:Problemset/1200-Minimum%20Absolute%20Difference/README.md#1200-minimum-absolute-difference
1865+
[1202]:Problemset/1202-Smallest%20String%20With%20Swaps/README.md#1202-smallest-string-with-swaps
18641866
[1207]:Problemset/1207-Unique%20Number%20of%20Occurrences/README.md#1207-unique-number-of-occurrences
18651867
[1208]:Problemset/1208-Get%20Equal%20Substrings%20Within%20Budget/README.md#1208-get-equal-substrings-within-budget
18661868
[1209]:Problemset/1209-Remove%20All%20Adjacent%20Duplicates%20in%20String%20II/README.md#1209-remove-all-adjacent-duplicates-in-string-ii
@@ -3105,6 +3107,7 @@
31053107
[1190l]:https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/
31063108
[1191l]:https://leetcode.com/problems/k-concatenation-maximum-sum/
31073109
[1200l]:https://leetcode.com/problems/minimum-absolute-difference/
3110+
[1202l]:https://leetcode.com/problems/smallest-string-with-swaps/
31083111
[1207l]:https://leetcode.com/problems/unique-number-of-occurrences/
31093112
[1208l]:https://leetcode.com/problems/get-equal-substrings-within-budget/
31103113
[1209l]:https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@
620620
[1190][1190l]|[反转每对括号间的子串][1190] |![rs]
621621
[1191][1191l]|[K 次串联后最大子数组之和][1191] |![rb]&nbsp;&nbsp;![rs]
622622
[1200][1200l]|[最小绝对差][1200] |![rs]
623+
[1202][1202l]|[交换字符串中的元素][1202] |![py]
623624
[1207][1207l]|[独一无二的出现次数][1207] |![rs]
624625
[1208][1208l]|[尽可能使字符串相等][1208] |![rb]&nbsp;&nbsp;![rs]
625626
[1209][1209l]|[删除字符串中的所有相邻重复项 II][1209] |![rs]
@@ -1861,6 +1862,7 @@
18611862
[1190]:Problemset/1190-Reverse%20Substrings%20Between%20Each%20Pair%20of%20Parentheses/README_CN.md#1190-反转每对括号间的子串
18621863
[1191]:Problemset/1191-K-Concatenation%20Maximum%20Sum/README_CN.md#1191-k-次串联后最大子数组之和
18631864
[1200]:Problemset/1200-Minimum%20Absolute%20Difference/README_CN.md#1200-最小绝对差
1865+
[1202]:Problemset/1202-Smallest%20String%20With%20Swaps/README_CN.md#1202-交换字符串中的元素
18641866
[1207]:Problemset/1207-Unique%20Number%20of%20Occurrences/README_CN.md#1207-独一无二的出现次数
18651867
[1208]:Problemset/1208-Get%20Equal%20Substrings%20Within%20Budget/README_CN.md#1208-尽可能使字符串相等
18661868
[1209]:Problemset/1209-Remove%20All%20Adjacent%20Duplicates%20in%20String%20II/README_CN.md#1209-删除字符串中的所有相邻重复项-ii
@@ -3105,6 +3107,7 @@
31053107
[1190l]:https://leetcode.cn/problems/reverse-substrings-between-each-pair-of-parentheses/
31063108
[1191l]:https://leetcode.cn/problems/k-concatenation-maximum-sum/
31073109
[1200l]:https://leetcode.cn/problems/minimum-absolute-difference/
3110+
[1202l]:https://leetcode.cn/problems/smallest-string-with-swaps/
31083111
[1207l]:https://leetcode.cn/problems/unique-number-of-occurrences/
31093112
[1208l]:https://leetcode.cn/problems/get-equal-substrings-within-budget/
31103113
[1209l]:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string-ii/

0 commit comments

Comments
 (0)