[LeetCode][Python3] 854. K-Similar Strings
2019. 8. 15. 02:52 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/k-similar-strings/
My Solution :
class Solution:
def kSimilarity(self, A: str, B: str) -> int:
if A == B:
return 0
checked = set([A])
queue = [A]
ans = 0
while queue:
next_queue = []
for S in queue:
S = list(S)
for i in range(len(S)):
if S[i] != B[i]:
break
for j in range(i+1, len(S)):
if S[j] == B[i] and S[j] != S[i]:
S[i], S[j] = S[j], S[i]
candidate = ''.join(S)
if candidate not in checked:
if candidate == B:
return ans + 1
checked.add(candidate)
next_queue.append(candidate)
S[i], S[j] = S[j], S[i]
queue = next_queue
ans += 1
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 980. Unique Paths III (0) | 2019.08.20 |
---|---|
[LeetCode][Python3] 47. Permutations II (0) | 2019.08.18 |
[LeetCode][Python3] 1079. Letter Tile Possibilities (0) | 2019.08.16 |
[LeetCode][Python3] 765. Couples Holding Hands (0) | 2019.08.15 |
[LeetCode][Python3] 5. Longest Palindromic Substring (0) | 2019.04.30 |
[LeetCode][Python3] 140. Word Break II (0) | 2019.04.25 |
[LeetCode][Python3] 50. Pow(x, n) (0) | 2019.04.24 |
[LeetCode][Python3] 212. Word Search II (0) | 2019.04.24 |
최근에 달린 댓글 최근에 달린 댓글