Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
lexicographically-smallest-equivalent-string.py 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
# Time: O(nlog*n) ~= O(n), n is the length of S
# Space: O(n)
class UnionFind(object):
def __init__(self, n):
self.set = range(n)
def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x]
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root == y_root:
return False
self.set[max(x_root, y_root)] = min(x_root, y_root)
return True
class Solution(object):
def smallestEquivalentString(self, A, B, S):
"""
:type A: str
:type B: str
:type S: str
:rtype: str
"""
union_find = UnionFind(26)
for i in xrange(len(A)):
union_find.union_set(ord(A[i])-ord('a'), ord(B[i])-ord('a'))
result = []
for i in xrange(len(S)):
parent = union_find.find_set(ord(S[i])-ord('a'))
result.append(chr(parent+ord('a')))
return "".join(result)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助