Skip to content

Commit 5f07b8a

Browse files
committed
Adding solution to '1061. Lexicographically Smallest Equivalent String' (medium).
1 parent 3886fa6 commit 5f07b8a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:
3+
p = {}
4+
def find(a):
5+
if p[a] != a:
6+
return find(p[a])
7+
return p[a]
8+
def union(a, b):
9+
pa = find(a)
10+
pb = find(b)
11+
if pa != pb:
12+
if pa < pb:
13+
p[pb] = pa
14+
else:
15+
p[pa] = pb
16+
17+
for ch in string.ascii_lowercase:
18+
p[ch] = ch
19+
for i in range(len(s1)):
20+
union(s1[i], s2[i])
21+
return ''.join(find(ch) for ch in baseStr)

0 commit comments

Comments
 (0)