Skip to content

Commit d5bf47b

Browse files
committed
Jun 05
1 parent 0cf0f1c commit d5bf47b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class UnionFind:
2+
def __init__(self):
3+
self.parent = dict()
4+
5+
def find(self, x: str) -> str:
6+
if x not in self.parent:
7+
self.parent[x] = x
8+
if x != self.parent[x]:
9+
self.parent[x] = self.find(self.parent[x])
10+
return self.parent[x]
11+
12+
def union(self, x: str, y: str) -> None:
13+
root_x = self.find(x)
14+
root_y = self.find(y)
15+
if root_x < root_y:
16+
self.parent[root_y] = root_x
17+
else:
18+
self.parent[root_x] = root_y
19+
20+
21+
class Solution:
22+
def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:
23+
union_find = UnionFind()
24+
for ch1, ch2 in zip(s1, s2):
25+
union_find.union(ch1, ch2)
26+
return ''.join([union_find.find(ch) for ch in baseStr])
27+
28+
29+
def main():
30+
s1 = 'parker'
31+
s2 = 'morris'
32+
baseStr = 'parser'
33+
assert Solution().smallestEquivalentString(s1, s2, baseStr) == 'makkek'
34+
35+
s1 = 'hello'
36+
s2 = 'world'
37+
baseStr = 'hold'
38+
assert Solution().smallestEquivalentString(s1, s2, baseStr) == 'hdld'
39+
40+
s1 = 'leetcode'
41+
s2 = 'programs'
42+
baseStr = 'sourcecode'
43+
assert Solution().smallestEquivalentString(s1, s2, baseStr) == 'aauaaaaada'
44+
45+
46+
if __name__ == '__main__':
47+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| June 02 | [135. Candy](https://leetcode.com/problems/candy/) | Hard | Solved |
99
| June 03 | [1298. Maximum Candies You Can Get from Boxes](https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/) | Hard | Solved |
1010
| June 04 | [3403. Find the Lexicographically Largest String From the Box I](https://leetcode.com/problems/find-the-lexicographically-largest-string-from-the-box-i/) | Medium | Solved |
11-
| June 05 | []() | | |
11+
| June 05 | [](1061. Lexicographically Smallest Equivalent String) | Medium | Unsolved |
1212
| June 06 | []() | | |
1313
| June 07 | []() | | |
1414
| June 08 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 0 | 0 | 0 |
43-
| Medium | 2 | 1 | 1 |
43+
| Medium | 3 | 1 | 2 |
4444
| Hard | 2 | 2 | 0 |

0 commit comments

Comments
 (0)