Skip to content

Commit 3dfb525

Browse files
committed
060625
1 parent 1d6784f commit 3dfb525

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# leetcode submit region begin(Prohibit modification and deletion)
2+
class Solution:
3+
def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:
4+
parent = [-1] * 26
5+
N = min(len(s1), len(s2))
6+
7+
def find(i):
8+
while parent[i] >= 0:
9+
i = parent[i]
10+
return i
11+
12+
def union(i, j):
13+
if i == j:
14+
return
15+
if j < i: # lexicographically smaller
16+
return union(find(j), find(i))
17+
parent[i] += parent[j]
18+
parent[j] = i
19+
20+
for i in range(N):
21+
c1 = ord(s1[i]) - ord('a')
22+
c2 = ord(s2[i]) - ord('a')
23+
union(find(c1), find(c2))
24+
ans = []
25+
for i in range(len(baseStr)):
26+
c1 = ord(baseStr[i]) - ord('a')
27+
ans.append(chr(find(c1) + ord('a')))
28+
return "".join(ans)
29+
30+
31+
# leetcode submit region end(Prohibit modification and deletion)
32+
33+
34+
class LexicographicallySmallestEquivalentString(Solution):
35+
pass

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from leetcode.editor.en.Q3403.FindTheLexicographicallyLargestStringFromTheBoxI import \
2-
FindTheLexicographicallyLargestStringFromTheBoxI
1+
from leetcode.editor.en.Q1061.LexicographicallySmallestEquivalentString import LexicographicallySmallestEquivalentString
32

43
if __name__ == '__main__':
5-
print(FindTheLexicographicallyLargestStringFromTheBoxI().answerString("gggg", 4))
4+
print(LexicographicallySmallestEquivalentString().smallestEquivalentString(s1="parker", s2="morris",
5+
baseStr="parser"))

0 commit comments

Comments
 (0)