Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) DP = [[0 for j in range(n + 1)] for i in range(m + 1)] res = [] for i in range(m + 1): DP[i][0] = i for j in range(n + 1): DP[0][j] = j for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: DP[i][j] = 1 + DP[i - 1][j - 1] else: if DP[i - 1][j] < DP[i][j - 1]: DP[i][j] = 1 + DP[i - 1][j] else: DP[i][j] = 1 + DP[i][j - 1] #print(DP[m][n]) #build string from DP array i = m j = n res = [] while i > 0 or j > 0: if i - 1 >= 0 and j - 1 >= 0 and str1[i - 1] == str2[j - 1]: res.append(str1[i - 1]) i -= 1 j -= 1 else: if DP[i - 1][j] < DP[i][j - 1]: res.append(str1[i - 1]) i -= 1 else: res.append(str2[j - 1]) j -= 1 return ''.join(reversed(res))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: cache = [[0 for i in range(len(str2)+1)] for j in range(len(str1)+1)] for i in range(1, len(str1)+1): for j in range(1, len(str2)+1): if str1[i-1] == str2[j-1]: cache[i][j] = 1+cache[i-1][j-1] else: cache[i][j] = max(cache[i][j-1], cache[i-1][j]) print(cache) i,j = len(str1), len(str2) ans = '' while i>0 and j>0: if str1[i-1] == str2[j-1]: ans = str1[i-1] + ans i -= 1 j -= 1 elif cache[i][j-1] > cache[i-1][j]: ans = str2[j-1] + ans j -= 1 else: ans = str1[i-1]+ans i -= 1 print(str1, str2, ans, i, j) if i > 0: ans = str1[:i] + ans i -= 1 if j > 0: ans = str2[:j] + ans j -= 1 return ans
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: #https://leetcode.com/problems/shortest-common-supersequence/discuss/639784/Python-DP-with-detailed-explanation #https://leetcode.com/problems/shortest-common-supersequence/discuss/674175/Python-Very-simple-solution-with-DP-Table-Memory-95 m,n = len(str1)+1, len(str2)+1 dp = ['']*n for c in range(n): dp[c] = str2[:c] for r in range(1, m): pre = dp[:] #use pre to store last row's information dp[0] += str1[r-1] for c in range(1, n): if str1[r-1] == str2[c-1]: dp[c] = pre[c-1]+str1[r-1] else: if len(pre[c]) < len(dp[c-1]): dp[c] = pre[c] + str1[r-1] else: dp[c] = dp[c-1] + str2[c-1] return dp[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n = len(str1) m = len(str2) dp = [[-1] * (m + 1) for _ in range(n + 1)] for i in range(n + 1): for j in range(m + 1): if i == 0 or j == 0: dp[i][j] = 0 else: if str1[i - 1] == str2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) i = n j = j result = [] while i > 0 and j > 0: if str1[i - 1] == str2[j - 1]: result.append(str1[i - 1]) i -= 1 j -= 1 else: left = dp[i][j - 1] top = dp[i - 1][j] if left > top: result.append(str2[j - 1]) j -= 1 else: result.append(str1[i - 1]) i -= 1 while i > 0: result.append(str1[i - 1]) i -= 1 while j > 0: result.append(str2[j - 1]) j -= 1 result = ''.join(result[::-1]) return result
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: str1 = '0' + str1 str2 = '0' + str2 len_1, len_2 = len(str1), len(str2) dp = [[0] * len_2 for _ in range(len_1)] for i in range(1, len_1): for j in range(1, len_2): if str1[i] == str2[j]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) res = '' m, n = len_1 - 1, len_2 - 1 while m > 0 or n > 0: if m == 0: res += str2[n] n -= 1 elif n == 0: res += str1[m] m -= 1 elif str1[m] == str2[n]: res += str1[m] m -= 1 n -= 1 elif dp[m-1][n] == dp[m][n]: res += str1[m] m -= 1 elif dp[m][n-1] == dp[m][n]: res += str2[n] n -= 1 # print(res) return res[::-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n = len(str1) m = len(str2) dp = [[-1] * (m + 1) for _ in range(n + 1)] for i in range(n + 1): for j in range(m + 1): if i == 0 or j == 0: dp[i][j] = 0 else: if str1[i - 1] == str2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]) i = n j = m result = [] while i > 0 and j > 0: if str1[i - 1] == str2[j - 1]: result.append(str1[i - 1]) i -= 1 j -= 1 else: left = dp[i][j - 1] top = dp[i - 1][j] if left > top: result.append(str2[j - 1]) j -= 1 else: result.append(str1[i - 1]) i -= 1 while i > 0: result.append(str1[i - 1]) i -= 1 while j > 0: result.append(str2[j - 1]) j -= 1 result = ''.join(result[::-1]) return result
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: longest_common_sub = '' str1_len = len(str1) str2_len = len(str2) dp = [[0 for _ in range(str2_len + 1)] for _ in range(str1_len + 1)] for i in range(str1_len + 1): dp[i][0] = i for j in range(str2_len + 1): dp[0][j] = j for i in range(1,str1_len+1): for j in range(1, str2_len+1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1 # reconstruct the array toret = [] i = str1_len j = str2_len while i > 0 or j > 0: if i > 0 and j > 0 and str1[i-1] == str2[j-1]: toret.append(str1[i-1]) i -= 1 j -= 1 else: if dp[i-1][j] < dp[i][j-1]: toret.append(str1[i-1]) i -= 1 else: toret.append(str2[j-1]) j -= 1 return ''.join(reversed(toret))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i, c in enumerate(s1): for j, d in enumerate(s2): dp[i + 1][j + 1] = 1 + dp[i][j] if c == d else max(dp[i + 1][j], dp[i][j + 1]) i, j, stk = m - 1, n - 1, [] while i >= 0 and j >= 0: if s1[i] == s2[j]: stk.append(s1[i]) i -= 1 j -= 1 elif dp[i + 1][j] < dp[i][j + 1]: stk.append(s1[i]) i -= 1 else: stk.append(s2[j]) j -= 1 return s1[: i + 1] + s2[: j + 1] + ''.join(reversed(stk))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s: str, t: str) -> str: dp =[[-1 for _ in range(len(t)+1)] for _ in range(len(s)+1)] for i in range(len(s)+1): for j in range(len(t)+1): if i==0 or j==0: dp[i][j]=0 elif s[i-1]==t[j-1]: dp[i][j] = dp[i-1][j-1]+1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) # for i in dp: # print(i) lcs ='' i,j=len(s),len(t) while i>0 and j>0: if s[i-1]==t[j-1]: lcs += s[i-1] i-=1 j-=1 else: if dp[i][j-1]>dp[i-1][j]: j-=1 lcs += t[j] else: i-=1 lcs+=s[i] while i>0: i-=1 lcs+=s[i] while j>0: j-=1 lcs +=t[j] return lcs[::-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: M, N = len(str1), len(str2) dp = [['']*(N+1) for _ in range(M+1)] ## TLE... # for i in range(M): # dp[i+1][0] = str1[:i+1] # for i in range(N): # dp[0][i+1] = str2[:i+1] # for i in range(1, M+1): # for j in range(1, N+1): # if str1[i-1]==str2[j-1]: # dp[i][j] = dp[i-1][j-1] + str1[i-1] # else: # if len(dp[i][j-1]) < len(dp[i-1][j]): # dp[i][j] = dp[i][j-1] + str2[j-1] # else: # dp[i][j] = dp[i-1][j] + str1[i-1] # # dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + 1 # return dp[M][N] for i in range(1, M+1): for j in range(1, N+1): if str1[i-1]==str2[j-1]: dp[i][j] = dp[i-1][j-1] + str1[i-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1], key=len) LCS = dp[M][N] res = '' i = j = 0 for c in LCS: while str1[i]!=c: res += str1[i] i+=1 while str2[j]!=c: res += str2[j] j+=1 res += c i+=1 j+=1 res += str1[i:] res += str2[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) dp = [[''] * n for _ in range(m)] for i in range(m): for j in range(n): if s1[i] == s2[j]: dp[i][j] = dp[i-1][j-1] + s1[i] if i > 0 and j > 0 else s1[i] else: if i > 0 and j > 0: dp[i][j] = max(dp[i-1][j], dp[i][j-1], key=len) elif i > 0: dp[i][j] = dp[i-1][j] elif j > 0: dp[i][j] = dp[i][j-1] res, i, j = '', 0, 0 for ch in dp[-1][-1]: while s1[i] != ch: res += s1[i] i += 1 while s2[j] != ch: res += s2[j] j += 1 res, i, j = res + ch, i + 1, j + 1 res += s1[i:] + s2[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i, c in enumerate(s1): for j, d in enumerate(s2): dp[i + 1][j + 1] = 1 + dp[i][j] if c == d else max(dp[i + 1][j], dp[i][j + 1]) i, j, stk = m - 1, n - 1, [] while i >= 0 and j >= 0: if s1[i] == s2[j]: stk.append(s1[i]) i -= 1 j -= 1 elif dp[i + 1][j] < dp[i][j + 1]: stk.append(s1[i]) i -= 1 else: stk.append(s2[j]) j -= 1 return s1[: i + 1] + s2[: j + 1] + ''.join(reversed(stk))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: n1, n2 = len(str1), len(str2) def lcs(): dp = [[''] * (n2 + 1) for _ in range(n1 + 1)] for i in range(1, n1 + 1): for j in range(1, n2 + 1): if str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + str1[i-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1], key=len) return dp[-1][-1] i = j = 0 res = '' for ch in lcs(): while i < n1 and str1[i] != ch: res += str1[i] i += 1 while j < n2 and str2[j] != ch: res += str2[j] j += 1 res += ch i += 1 j += 1 return res + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: @lru_cache(None) def lcs(x, y): m, n = len(x), len(y) dp = [['' for _ in range(n+1)] for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): if x[i-1] == y[j-1]: dp[i][j] = dp[i-1][j-1] + x[i-1] else: dp[i][j] = max(dp[i][j-1], dp[i-1][j], key=len) return dp[-1][-1] # print(lcs(str1, str2)) lcs_ = lcs(str1, str2) i = 0 j = 0 res = '' for c in lcs_: while str1[i] != c: res += str1[i] i += 1 while str2[j] != c: res += str2[j] j += 1 res += c i += 1 j += 1 # print(res) return res + str1[i:] + str2[j:]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: # build dp for LCS dp = [[0]*(len(str1)+1) for _ in range(len(str2)+1)] for r in range(1, len(dp)): for c in range(1, len(dp[0])): diag = dp[r-1][c-1] up = dp[r-1][c] left = dp[r][c-1] if str1[c-1] == str2[r-1]: diag += 1 dp[r][c] = max(diag, up, left) # build supersequence by going up the dp grid r = len(str2) c = len(str1) res = [] while r > 0 and c > 0: if str1[c-1] == str2[r-1]: res.append(str1[c-1]) r -= 1 c -= 1 else: # move up/left depending on which dp is larger up = dp[r-1][c] left = dp[r][c-1] if up > left: # move up res.append(str2[r-1]) r -= 1 else: # move left res.append(str1[c-1]) c -= 1 while r > 0: res.append(str2[r-1]) r -= 1 while c > 0: res.append(str1[c-1]) c -= 1 return ''.join(res[::-1])
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: if len(str2) > len(str1): str1, str2 = str2, str1 if not str1 or not str2: return str1 m, n = len(str1), len(str2) dp = [[0] * (m+1) for _ in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): if str2[i-1] == str1[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) #print(dp) final_str = '' i, j = n, m c1 = str1[j-1] c2 = str2[i-1] while i > 0 or j > 0: #print(f'{i} {c2} {j} {c1}') if c1 == c2: final_str += c1 i -= 1 j -= 1 c1 = str1[j-1] if j > 0 else '' c2 = str2[i-1] if i > 0 else '' else: if i == 0 or dp[i][j-1] > dp[i-1][j]: final_str += c1 j -= 1 c1 = str1[j-1] if j > 0 else '' else: final_str += c2 i -= 1 c2 = str2[i-1] if i > 0 else '' return ''.join(reversed(final_str))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m = len(str1) n = len(str2) dp = [[-1]*(n+1) for i in range(m+1)] for i in range(m+1): dp[i][0]=0 for j in range(n+1): dp[0][j]=0 for i in range(1, m+1): for j in range(1, n+1): if str1[i-1]==str2[j-1]: dp[i][j]=1+dp[i-1][j-1] else: dp[i][j]=max(dp[i-1][j], dp[i][j-1]) i = m j = n s = '' while(i>0 and j>0): if str1[i-1]==str2[j-1]: s = s+str1[i-1] i = i-1 j = j-1 else: if dp[i-1][j]>dp[i][j-1]: s = s+str1[i-1] i = i-1 else: s=s+str2[j-1] j = j-1 while(i>0): s = s+str1[i-1] i = i-1 while(j>0): s= s+str2[j-1] j = j-1 return s[::-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, s1: str, s2: str) -> str: m, n = len(s1), len(s2) dp = [[''] * n for _ in range(m)] for i in range(m): for j in range(n): if s1[i] == s2[j]: dp[i][j] = dp[i-1][j-1] + s1[i] if i > 0 and j > 0 else s1[i] else: if i > 0 and j > 0: dp[i][j] = max(dp[i-1][j], dp[i][j-1], key=len) elif i > 0: dp[i][j] = dp[i-1][j] elif j > 0: dp[i][j] = dp[i][j-1] res, i, j = '', 0, 0 for ch in dp[-1][-1]: while i < len(s1) and s1[i] != ch: res += s1[i] i += 1 while j < len(s2) and s2[j] != ch: res += s2[j] j += 1 res += ch i += 1 j += 1 res += s1[i:] + s2[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: ''' @params: two strings str1 and str2 @return: shortest common supersequence ''' # Use Dynamic Programming to first compute shortest common subsequence m = len(str1) n = len(str2) dp = [[0] * (n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): # Case 1: delete text1[i] in common subsequence deletion = dp[i-1][j] # Case 2: delete text2[j] in common subsequence insertion = dp[i][j-1] # Case 3: match --> common subsequence extend by 1 match = dp[i-1][j-1] + 1 # Case 4: mistch --> common subsequence keeps the same mismatch = dp[i-1][j-1] if str1[i-1] == str2[j-1]: dp[i][j] = max(deletion, insertion, match) else: dp[i][j] = max(deletion, insertion, mismatch) # Backtrack from dp[m][n] to construct shortest common supersequence # in revered order i, j = m, n ans = [] while i > 0 or j > 0: # Case 1: str1 is used up if i == 0: ans.append(str2[j-1]) j -= 1 # Case 2: str2 is used up elif j == 0: ans.append(str1[i-1]) i -= 1 # Case 3: match elif str1[i-1] == str2[j-1]: ans.append(str1[i-1]) i -= 1 j -= 1 # Case 4: insertion of str1 elif dp[i][j] == dp[i-1][j]: ans.append(str1[i-1]) i -= 1 # Case 5: deletion of str2 elif dp[i][j] == dp[i][j-1]: ans.append(str2[j-1]) j -= 1 #print('i, j: ', i, j) #print(''.join(reversed(ans))) return ''.join(reversed(ans))
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) s = [] i, j = m, n while i > 0 and j > 0: if str1[i - 1] == str2[j - 1]: s.append(str1[i - 1]) i -= 1 j -= 1 elif dp[i][j - 1] > dp[i - 1][j]: s.append(str2[j - 1]) j -= 1 else: s.append(str1[i - 1]) i -= 1 while j > 0: s.append(str2[j - 1]) j -= 1 while i > 0: s.append(str1[i - 1]) i -= 1 s.reverse() return ''.join(s)
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, a: str, b: str) -> str: def lcs(a,b): n, m = len(a),len(b) dp = [['' for j in range(m+1)] for i in range(n+1)] for i in range(n): for j in range(m): if a[i]==b[j]: dp[i+1][j+1]=dp[i][j]+a[i] else: dp[i+1][j+1]=max(dp[i+1][j], dp[i][j+1] , key =len) return dp[-1][-1] res , i ,j = '',0,0 for c in lcs(a,b): while a[i]!=c: res+=a[i] i+=1 while b[j]!=c: res+=b[j] j+=1 res+=c i+=1 j+=1 res+= a[i:]+b[j:] return res
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m=len(str1) n=len(str2) dp = [[0 for x in range(n+1)] for x in range(m+1)] for i in range(m+1): for j in range(n+1): if i == 0 or j == 0: dp[i][j] = 0 elif str1[i-1] == str2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) s='' i = m j = n while i > 0 and j > 0: if str1[i-1] == str2[j-1]: s += str1[i-1] i-=1 j-=1 elif dp[i-1][j] > dp[i][j-1]: i-=1 else: j-=1 res=s[::-1] i,j,st=0,0,0 final='' while(i<m and j<n and st<len(res)): if str1[i]!=res[st]: final+=str1[i] i+=1 if str2[j]!=res[st]: final+=str2[j] j+=1 elif str1[i]==res[st]: final+=res[st] i+=1 j+=1 st+=1 final+=str2[j:]+str1[i:] return final
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: size1, size2 = len(str1), len(str2) dp = [None] * (size2 + 1) dp[0] = '' for i in range(1, size2 + 1): dp[i] = dp[i-1] + str2[i-1] for i1 in range(1, size1 + 1): newDP = [None] * (size2 + 1) newDP[0] = dp[0] + str1[i1-1] for i2 in range(1, size2 + 1): if str1[i1-1] == str2[i2-1]: newDP[i2] = dp[i2-1] + str1[i1-1] else: newDP[i2] = min( dp[i2] + str1[i1-1], newDP[i2-1] + str2[i2-1], key=len) dp = newDP return dp[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: size1, size2 = len(str1), len(str2) Inf = str1 + str2 dp = [None] * (size2 + 1) dp[0] = '' for i in range(1, size2 + 1): dp[i] = dp[i-1] + str2[i-1] for i1 in range(1, size1 + 1): newDP = [None] * (size2 + 1) newDP[0] = dp[0] + str1[i1-1] for i2 in range(1, size2 + 1): if str1[i1-1] == str2[i2-1]: newDP[i2] = dp[i2-1] + str1[i1-1] else: newDP[i2] = min( dp[i2] + str1[i1-1], newDP[i2-1] + str2[i2-1], key=len) dp = newDP return dp[-1]
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) dp = [[0]*(n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): if str1[i-1]==str2[j-1]: dp[i][j] = dp[i-1][j-1]+1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) i, j = m, n lcs = [] while i>0 and j>0: if str1[i-1]==str2[j-1]: lcs.append(str1[i-1]) i -= 1 j -= 1 else: if dp[i-1][j]>dp[i][j-1]: i -= 1 else: j -= 1 lcs.reverse() insert_1 = defaultdict(list) idx = 0 for i in range(m): if idx<len(lcs) and str1[i]==lcs[idx]: idx += 1 else: insert_1[idx].append(str1[i]) insert_2 = defaultdict(list) idx = 0 for i in range(n): if idx<len(lcs) and str2[i]==lcs[idx]: idx += 1 else: insert_2[idx].append(str2[i]) ans = '' for i in range(idx): ans += ''.join(insert_1[i])+''.join(insert_2[i])+lcs[i] ans += ''.join(insert_1[idx])+''.join(insert_2[idx]) return ans
Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them. (A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.)   Example 1: Input: str1 = "abac", str2 = "cab" Output: "cabac" Explanation: str1 = "abac" is a subsequence of "cabac" because we can delete the first "c". str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac". The answer provided is the shortest such string that satisfies these properties.   Note: 1 <= str1.length, str2.length <= 1000 str1 and str2 consist of lowercase English letters.
class Solution: def shortestCommonSupersequence(self, str1: str, str2: str) -> str: m, n = len(str1), len(str2) @lru_cache(None) def dp(i, j): if i == m or j == n: return 0 if str1[i] == str2[j]: return 1 + dp(i + 1, j + 1) return max(dp(i + 1, j), dp(i, j + 1)) dp(0, 0) i = 0 j = 0 result = '' while i < m and j < n: if str1[i] == str2[j]: result += str1[i] i += 1 j += 1 else: s1 = dp(i + 1, j) s2 = dp(i, j + 1) if s1 > s2: result += str1[i] i += 1 else: result += str2[j] j += 1 if i < m: result += str1[i:] if j < n: result += str2[j:] return result
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like ? or *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "*" Output: true Explanation: '*' matches any sequence. Example 3: Input: s = "cb" p = "?a" Output: false Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. Example 4: Input: s = "adceb" p = "*a*b" Output: true Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". Example 5: Input: s = "acdcb" p = "a*c?b" Output: false
class Solution: def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ '''维护两个下标,逐个比较,如果pj为*,则记录*的位置,将*后一个元素与si进行比较,如果不相等,则将i从记录的位置+1,重新比较''' i=0 j=0 star=-1 lenp=len(p) while i<len(s): if j<lenp and (s[i]==p[j] or p[j]=='?'): i+=1 j+=1 elif j<lenp and p[j]=='*': star=j mi=i j+=1 elif star!=-1: mi+=1 i=mi j=star+1 else: return False while j<lenp and p[j]=='*': j+=1 return j==lenp
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like ? or *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "*" Output: true Explanation: '*' matches any sequence. Example 3: Input: s = "cb" p = "?a" Output: false Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. Example 4: Input: s = "adceb" p = "*a*b" Output: true Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". Example 5: Input: s = "acdcb" p = "a*c?b" Output: false
class Solution: def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ m = len(s) n = len(p) starj = -1 last_match = -1 i = j = 0 while i<m: if j<n and (s[i]==p[j] or p[j]=='?'): i+=1 j+=1 elif j<n and p[j]=='*': starj = j j += 1 last_match = i elif starj!=-1: j = starj+1 last_match +=1 i = last_match else: return False while j<n and p[j]=='*': j+=1 return j==n
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like ? or *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "*" Output: true Explanation: '*' matches any sequence. Example 3: Input: s = "cb" p = "?a" Output: false Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. Example 4: Input: s = "adceb" p = "*a*b" Output: true Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". Example 5: Input: s = "acdcb" p = "a*c?b" Output: false
class Solution: def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ sp = 0 pp = 0 match = 0 star = -1 while sp < len(s): if (pp < len(p) and (s[sp] == p[pp] or p[pp] == '?')): sp +=1 pp +=1 elif pp < len(p) and p[pp] == '*': star = pp match = sp pp +=1 elif star != -1: pp = star + 1 match +=1 sp = match else: return False while(pp < len(p) and p[pp] == '*'): pp += 1 return pp == len(p)
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters like ? or *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "*" Output: true Explanation: '*' matches any sequence. Example 3: Input: s = "cb" p = "?a" Output: false Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'. Example 4: Input: s = "adceb" p = "*a*b" Output: true Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce". Example 5: Input: s = "acdcb" p = "a*c?b" Output: false
class Solution: def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ len_s = len(s) len_p = len(p) if len_s == 0 and len_p == 0: return True elif len_p == 0: return False elif len_p == p.count('*'): return True elif len_p - p.count('*') > len_s: return False i_s = 0 i_p = 0 star_index = None s_star_match = None while i_s < len_s: pchar = p[i_p] if i_p < len_p else None schar = s[i_s] if pchar == '*': star_index = i_p s_star_match = i_s i_p += 1 elif pchar == '?' or pchar == schar: i_s +=1 i_p += 1 elif star_index is not None: i_p = star_index + 1 s_star_match += 1 i_s = s_star_match else: return False while i_p < len_p and p[i_p] == '*': i_p += 1 return i_p == len_p
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: a = '' b = '' for i in range(len(A)): if A[i] != B[i]: a+=A[i] b+=B[i] return self.dfs(a,b) def dfs(self,a,b): if not a: return 0 one = [] two = [] for i in range(len(a)): if a[0] == b[i]: one.append(i) if b[0] == a[i]: two.append(i) if two: i = two[0] c = a[1:i] + a[i+1:] d = b[1:i] + b[i+1:] return self.dfs(c,d) + 1 else: res = float('inf') for i in one: c = a[i] + a[1:i] + a[i+1:] d = b[:i]+b[i+1:] res= min(res,self.dfs(c,d)+1) return res
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 def neighbors(s, i=0): while s[i] == B[i]: i+= 1 for j in range(i+1, len(A)): if s[j] == B[i] and B[j] == s[i]: yield s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] return for j in range(i+1, len(A)): if s[j] == B[i] and s[j] != B[j]: yield s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] dp = collections.deque([(A,0)]) met = set() while dp: A_, step = dp.popleft() for n in neighbors(A_): if n == B: return step+1 if n not in met: dp.append((n,step+1)) met.add(n)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: lsta, lstb = '', '' for i in range(len(A)): if A[i]!=B[i]: lsta+=A[i] lstb+=B[i] q = [(0,lsta,lstb)] while q: time,la,lb = q.pop(0) if not lb: return time goal = lb[0] work,ls = None,[] for i in range(1,len(la)): if la[i]==goal and la[0]==lb[i]: work = True break if work: q.append((time+1,la[1:i]+la[i+1:],lb[1:i]+lb[i+1:])) else: for i in range(1,len(la)): if la[i]==goal: q.append((time+1,la[1:i]+la[0]+la[i+1:],lb[1:]))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: A = list(A) memo = {} def solution(i): t = tuple(A) if (i,t) in memo: return memo[i,t] if i == len(A): return 0 if A[i] == B[i]: return solution(i+1) mymin = float('inf') for j in range(i+1, len(A)): if A[j] != B[i]: continue A[i], A[j] = A[j], A[i] mymin = min(mymin, solution(i+1)) A[j], A[i] = A[i], A[j] memo[i,t] = 1 + mymin return 1 + mymin return solution(0)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def swapPos(string): i = 0 while string[i] == B[i]: i += 1 for j in range(i+1, length): if string[j] == B[i]: yield string[:i] + string[j] + string[i+1:j] + string[i] + string[j+1:] dq = deque([(A, 0)]) visited = set(A) length = len(A) while dq: string, dist = dq.popleft() if string == B: return dist for s2 in swapPos(string): if s2 not in visited: dq.append((s2, dist+1)) visited.add(s2)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: @lru_cache(maxsize=None) def kSimilarity(self, A: str, B: str) -> int: if not A and not B: return 0 if A[0]==B[0]: return self.kSimilarity(A[1:],B[1:]) ans=float('inf') for i, c in enumerate(B): if c==A[0]: ans=min(ans,1+self.kSimilarity(A[1:],B[1:i]+B[0]+B[i+1:])) return ans
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
# class Solution: # def kSimilarity(self, A: str, B: str) -> int: # n = len(A) # A, B = list(A), list(B) # mp = defaultdict(list) # for i, ch in enumerate(A): # mp[ch].append(i) # def dfs(idx): # if idx == n: # return 0 # if A[idx] == B[idx]: # return dfs(idx+1) # res = float('inf') # for nxt in range(idx+1,n): # if A[nxt] != B[idx]: # continue # A[idx], A[nxt] = A[nxt], A[idx] # res = min(res, dfs(idx+1)+1) # A[idx], A[nxt] = A[nxt], A[idx] # return res # return dfs(0) from functools import lru_cache class Solution: def kSimilarity(self, A: str, B: str) -> int: n = len(A) # mp = defaultdict(list) # for i, ch in enumerate(A): # mp[ch].append(i) @lru_cache(None) def dfs(s): if s == B: return 0 idx = 0 while s[idx] == B[idx]: idx += 1 res = float('inf') for nxt in range(idx+1,n): if s[nxt] != B[idx]: # if s[nxt] != B[idx] or s[nxt] == B[nxt]: continue res = min(res, dfs(s[:idx]+s[nxt]+s[idx+1:nxt]+s[idx]+s[nxt+1:])+1) return res return dfs(A)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: q = collections.deque() visited = set() q.append(A) visited.add(A) step = -1 while q: step += 1 lens = len(q) for _ in range(lens): curr = q.popleft() if curr == B: return step for next in self._swap(curr, B): # takes O(N) if next in visited: continue q.append(next) visited.add(next) def _swap(self, s, B): # now only takes O(N) i = 0 while s[i] == B[i]: # if S[i]==B[i], we don't need to swap them - strong prune to makes sure swapped string always get more and more similar with B i += 1 for j in range(i + 1, len(s)): if s[j] == B[i]: # since B[i]!=s[i], if we swap s[j] to s[i], now B[i]=s[i]: this is how every swap make sure we get more and more closer to B yield s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:]
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A, B): def nei(x): i = 0 while x[i] == B[i]: i += 1 for j in range(i + 1, len(A)): if x[j] == B[j]: continue if x[j] == B[i] and x[i] == B[j]: yield x[:i] + x[j] + x[i + 1:j] + x[i] + x[j + 1:] break if x[j] == B[i]: yield x[:i] + x[j] + x[i + 1:j] + x[i] + x[j + 1:] # def test(): stack = [(A, 0)] visit = {A} for cur, d in stack: # cur, d = stack.pop() if cur == B: return d for neighbor in nei(cur): if neighbor not in visit: visit.add(neighbor) stack.append((neighbor, d+1)) # n = len(A) # def nei(x): # i = 0 # while x[i] == B[i]: i += 1 # for j in range(i+1,n): # if x[j] == B[j]: # continue # if x[j] == B[i] and x[i] == B[j]: # yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] # break # if x[j] == B[i]: # yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] # pool = [(A,0)] # seen = {A} # for x,d in pool: # if x == B: return d # for y in nei(x): # if y not in seen: # seen.add(y) # pool.append((y,d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: # Just like the shuffle board game, consider each string as a node # And for each child is the shuffled result def kSimilarity(self, A: str, B: str) -> int: def find(s): i = 0 res = [] while s[i] == B[i]: i += 1 for j in range(i+1,len(B)): if s[j] == B[i]: res.append(s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:]) return res q = collections.deque([[0,A]]) seen = set([A]) while q: size = len(q) for _ in range(size): step,s = q.popleft() if s == B:return step for new in find(s): if new not in seen: seen.add(new) q.append([step+1,new]) return
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i]==B[i]: i+=1 for j in range(i+1, len(x)): if x[i]==B[j] and x[j] != B[j]: yield x[:i] + x[j] + x[i+1:j] + x[i] + x[j+1:] seen = {A} queue = [(A,0)] for node, change in queue: if node==B: return change for y in nei(node): if y not in seen: seen.add(node) queue.append((y, change+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i] == B[i]: i+=1 for j in range(i+1, len(x)): if x[j] == B[i]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] q, seen = [(A,0)], {A} for x, d in q: if x == B: return d for y in nei(x): if y not in seen: seen.add(y), q.append((y,d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def successor(s): i=0 while s[i] == B[i]: i += 1 for j in range(i, len(s)): if s[j] == B[i]: yield s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] q = deque([(A, 0)]) seen = set() while q: curr, dist = q.popleft() if curr == B: return dist if curr not in seen: seen.add(curr) q.extend([(suc, dist+1) for suc in successor(curr)])
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: N = len(A) def dfs(A, B, pos): if A == B: return 0 while A[pos] == B[pos]: pos += 1 minCnt = float('inf') for i in range(pos + 1, N): if B[i] == A[pos] and B[i] != A[i]: B[i], B[pos] = B[pos], B[i] tmp = dfs(A, B, pos + 1) + 1 minCnt = min(tmp, minCnt) B[i], B[pos] = B[pos], B[i] return minCnt return dfs(list(A), list(B), 0)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: A = list(A) B = list(B) n = len(A) # ans = 0 # for i in range(n): # if A[i]==B[i]: # continue # else: # ans+=1 # for j in range(i+1, n): # if B[j] == A[i]: # B[i], B[j] = B[j], B[i] # break # return ans def rec(i): if i==n: return 0 if A[i]==B[i]: return rec(i+1) else: min_ = sys.maxsize for j in range(i+1, n): if B[j] == A[i] and B[j]!=A[j]: B[i], B[j] = B[j], B[i] min_ = min(min_, rec(i+1) + 1) B[i], B[j] = B[j], B[i] return min_ return rec(0) # ans = 0 # for i in range(n): # if A[i]==B[i]: # continue # else: # ans+=1 # for j in range(i+1, n): # if B[j] == A[i] and B[j]!=A[j]: # B[i], B[j] = B[j], B[i] # break # return ans # def rec(i): # if i==n: # return 0 # if A[i]==B[i]: # return rec(i+1) # else: # min_ = sys.maxsize # j = 0 # while A[j]==B[j]: # j+=1 # for k in range(j+1, n): # if B[k] == A[j]: # B[j], B[k] = B[k], B[j] # min_ = min(min_, rec(i+1) + 1) # B[j], B[k] = B[k], B[j] # return min_ # return rec(0) # def nei(x): # i = 0 # while x[i] == B[i]: i+=1 # for j in range(i+1, len(x)): # if x[j] == B[i]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] # q, seen = [(A,0)], set([A]) # for x, d in q: # if x == B: return d # for y in nei(x): # if y not in seen: # seen.add(y), q.append((y,d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
''' 'ab' 'ba' 1 'abc' -> 'acb' -> 'bca' 'bca' 2 'abac' -> 'baac' -> 'baca' 'baca' 2 'aabc' -> 'acba' -> 'abca' 'abca' 2 * See the problem as a graph problem. * Nodes are the strings. * Two nodes are conneted to each order if the one string is equal to the other if we swap two chars. * The problem boils down to find the shortest path between A and B. * Since this is an unweigthed graph, the shortest path can be found by traversing the graph in BFS. 'abc' -> 'bac' -> 'cba' -> 'acb' * This approach doesn't sound an efficient approach because for every node we'd need to generated its adjcent nodes. * This sounds like an optimization problem. 'abcd' --> 'bcd' 'adbc' 'dbc' * If the first char from A and B are equal, recurse on the remaining string. * Else, swap A so it first char match and recurse. If the target char repeats, try swapping all of them and consider the one that leads to the smallest K. ('abac', 'baca') = 1 + ('aac', 'aca') ('aac', 'aca') = ('ac', 'ca') ('ac', 'ca') = 1 + ('a', 'a') ('a', 'a') = 0 ''' class Solution: def kSimilarity(self, A: str, B: str) -> int: return do_kSimilarity(list(A), list(B), {}) def do_kSimilarity(A, B, memo): if (tuple(A), tuple(B)) in memo: return memo[(tuple(A), tuple(B))] if A == B: return 0 if A[0] == B[0]: return do_kSimilarity(A[1:], B[1:], memo) else: swaps = [] for i, char in enumerate(A): if char == B[0]: swaps.append(i) k = float('inf') for swap in swaps: A[0], A[swap] = A[swap], A[0] k = min(k, 1 + do_kSimilarity(A[1:], B[1:], memo)) A[0], A[swap] = A[swap], A[0] memo[(tuple(A), tuple(B))] = k return k
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def neighbor(s): i = 0 while i<len(B) and s[i]==B[i]: i += 1 for j in range(i+1, len(B)): if s[j] == B[i]: yield s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] queue = deque([(A, 0)]) visited = {A} while queue: s, d = queue.popleft() if s == B: return d for n in neighbor(s): if n not in visited: visited.add(n) queue.append([n, d+1])
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def successor(s): i=0 while s[i] == B[i]: i += 1 ans = [] for j in range(i, len(s)): if s[j] == B[i]: ans.append(s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:]) return ans q = deque([(A, 0)]) seen = set() while q: curr, dist = q.popleft() if curr == B: return dist if curr not in seen: seen.add(curr) q.extend([(suc, dist+1) for suc in successor(curr)])
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def bfs(self, A: str, B: str): if A == B: return 0 n = len(A) q = [] visited = set() q.append((B, 0, 0)) while q: processed, idx, swaps = q.pop(0) visited.add(processed) while processed[idx] == A[idx]: idx += 1 for i in range(idx+1,n): if A[idx] == processed[i]: new_processed = processed[:idx] + processed[i] + processed[idx+1:i] + processed[idx] + processed[i+1:] if A == new_processed: return swaps + 1 if new_processed not in visited: q.append((new_processed, idx+1, swaps+1)) return 0 def kSimilarity(self, A: str, B: str) -> int: return self.bfs(A, B)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def neighbors(A): i = 0 while A[i] == B[i]: i += 1 for j in range(i+1, len(B)): if A[i] != A[j] and A[j] == B[i]: yield A[:i] + A[j] + A[i+1:j] + A[i] + A[j+1:] queue = [(A,0)] seen = set([A]) for s, val in queue: if s == B: return val for nxt_s in neighbors(s): if nxt_s not in seen: queue.append((nxt_s, val+1)) seen.add(nxt_s) return -1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: # Just like the shuffle board game, consider each string as a node # And for each child is the shuffled result which one step closer to the target # Another key is how to find the next step (by finding the first different letter and swap) # The order does not matter def kSimilarity(self, A: str, B: str) -> int: def find(s): i = 0 res = [] while s[i] == B[i]: i += 1 for j in range(i+1,len(B)): if s[j] == B[i]: res.append(s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:]) return res q = collections.deque([[0,A]]) seen = set([A]) while q: size = len(q) for _ in range(size): step,s = q.popleft() if s == B:return step for new in find(s): if new not in seen: seen.add(new) q.append([step+1,new]) return
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: self.min = float('inf') A = list(A) B = list(B) def swap(a,b): A[a], A[b] = A[b], A[a] @lru_cache(None) def helper(left, right, AB): ok = True swaps = 0 while A != B: ok = False a = A[left] b = B[right] if a != b: minn = float('inf') poss = [] for i,c in enumerate(A): if b == c and i >= left: poss.append(i) # if len(poss) > 1: for p in poss: swap(left,p) otherswaps = helper(left+1,right+1, tuple(A+B)) swap(left,p) minn = min(otherswaps, minn) return swaps+ 1 + minn # else: # if poss: # # print(A,B, a,b) # swap(left,poss[0]) # swaps += 1 left = (left+1) % len(A) right= (right+1) % len(B) return swaps # left = right = 0 # ok = True # swaps = 0 # while A != B: # ok = False # a = A[left] # b = B[right] # if a != b: # # indx = A.index(b) # for i,c in enumerate(A): # if b == c and i >= left: # indx = i # break # swap(left,indx) # swaps += 1 # left = (left+1) % len(A) # right= (right+1) % len(B) f = helper(0,0, tuple(A+B)) print(A) print(B) # out = helper(A, 0, 0) # print(out) return f
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: self.min = float('inf') A = list(A) B = list(B) def swap(a,b): A[a], A[b] = A[b], A[a] @lru_cache(None) def helper(left, right, AB): ok = True swaps = 0 while A != B: ok = False a = A[left] b = B[right] if a != b: minn = float('inf') poss = [] for i,c in enumerate(A): if b == c and i >= left: poss.append(i) # if len(poss) > 1: for p in poss: swap(left,p) otherswaps = helper(left+1,right+1, tuple(A+B)) swap(left,p) minn = min(otherswaps, minn) return swaps+ 1 + minn left = (left+1) % len(A) right= (right+1) % len(B) return swaps f = helper(0,0, tuple(A+B)) return f
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A==B: return 0 dq = deque([(A,0)]) cnt = 0 N = len(A) while dq: length = len(dq) for _ in range(length): curr, idx = dq.popleft() lst = list(curr) for i in range(idx, N): if lst[i]!=B[i]: for j in range(i+1, N): if lst[j]!=B[j] and lst[j]==B[i]: lst[i], lst[j] = lst[j], lst[i] temp = ''.join(lst) if temp==B: return cnt+1 dq.append((temp, i+1)) lst[i], lst[j] = lst[j], lst[i] break cnt += 1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dist(a, b): # count number of differing characters return sum( (1 if c!=d else 0 for (c,d) in zip(a,b) ) ) def swap(s, i, j): # swap ith and jth chars of string s # assert i != j if i > j: i, j = j, i return s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] def DFS(string, k): # can string be swapped into problem's str `A` in `k` moves? if string == A: return True if k == 0: return False for i,c in enumerate(string): if A[i] == c: continue # c is wrong, we need to swap a correct letter into it # only look for after current position to avoid double swaps for j in range(i, len(A)): if string[j] == A[i] and A[j] != A[i]: # try this swap if DFS(swap(string, i, j), k-1): return True # return False after trying to correct the first wrong letter: # if there is any `k` solution, then there's a `k` solution # that starts on this letter return False for k in range(len(A)): if DFS(B, k): return k
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: N = len(A) if not N: return 0 cnt = 0 q = deque() q.append((cnt, A, 0)) while q: cnt, v, i = q.popleft() if v == B: return cnt for j in range(i+1, N): while v[i]==B[i]: i += 1 if v[j]==B[i] and v[j]!=B[j]: candidate = v[:i]+v[j]+v[i+1:j]+v[i]+v[j+1:] q.append((cnt+1, candidate, i+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: A, B = list(A), list(B) N = len(A) if not N: return 0 cnt = 0 q = deque() q.append((cnt, A, 0)) while q: cnt, v, i = q.popleft() if v == B: return cnt for j in range(i+1, N): while v[i]==B[i]: i += 1 if v[j]==B[i] and v[j]!=B[j]: candidate = v[:] candidate[i], candidate[j] = candidate[j], candidate[i] q.append((cnt+1, candidate, i+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: self.counter = float('inf') A = list(A) B = list(B) def swap(first, cur): if first == len(A) - 1 or A[first:] == B[first:]: self.counter = min(self.counter, cur) #print(f'cur={cur}, first={first}') return for i in range(first, len(A)): if A[i] != B[i]: for k in range(i + 1, len(A)): if A[i] == B[k] and cur + 1 < self.counter: B[i], B[k] = B[k], B[i] #print(f'B={B}') swap(i + 1, cur + 1) B[i], B[k] = B[k], B[i] break return swap(0, 0) return self.counter
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dfs(A, B): if not A and not B: return 0 if str(A) == str(B): return 0 k = str(A) if k in dp: return dp[k] dp[k] = len(A) - 1 if A[0] == B[0]: dp[k] = dfs(A[1:], B[1:]) return dp[k] for j in range(1,len(A)): if A[j] == B[0]: A[0],A[j] = A[j],A[0] dp[k] = min(dp[k],1 + dfs(A[1:],B[1:])) A[j],A[0] = A[0],A[j] return dp[k] dp = dict() A,B = list(A),list(B) dp[str(A)] = dfs(A,B) return dp[str(A)]
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: N = len(A) if not N: return 0 cnt = 0 q = deque() q.append((cnt, A, 0)) while q: cnt, v, i = q.popleft() if v == B: return cnt for j in range(i+1, N): while v[i]==B[i]: i += 1 if v[j]==B[i] and v[j]!=B[j]: candidate = list(v) candidate[i], candidate[j] = candidate[j], candidate[i] candidate = ''.join(candidate) q.append((cnt+1, candidate, i+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: self.N = len(A) self.dict1 = collections.defaultdict() return self.dfs(list(A), list(B), 0) def dfs(self,A, B, pos): sB = ''.join(B) if sB in self.dict1: return self.dict1[sB] if A == B: return 0 while A[pos] == B[pos]: pos += 1 minCnt = float('inf') for i in range(pos + 1, self.N): if B[i] == A[pos] and B[i] != A[i]: B[i], B[pos] = B[pos], B[i] tmp = self.dfs(A, B, pos + 1) + 1 minCnt = min(tmp, minCnt) B[i], B[pos] = B[pos], B[i] self.dict1[sB] = minCnt return minCnt
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dist(a, b): # count number of differing characters return sum( (1 if c!=d else 0 for (c,d) in zip(a,b) ) ) def swap(s, i, j): # swap ith and jth chars of string s # assert i != j if i > j: i, j = j, i return s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] def DFS(string, k, seen): # print(f\" {string}\") if string in seen: return False, 0 seen.add(string) # print(f\" {k}: {string}\") # can string be swapped into problem's str `A` in `k` moves? if string == A: return True, 1 # early break if distance is too great: # best possible for string of length N: # N is even: N/2 (every swap corrects 2 letters # N is odd: N//2 + 1 # every swap until last 3 letters corrects 2 # last 3 letters take 2 swaps distance = dist(string, A) best = distance/2 if distance % 2 == 0 else (distance//2) + 1 if k < best: return False, 1 neighbors = [] for i,c in enumerate(string): if A[i] == c: continue # c is wrong, we need to swap a correct letter into it # only look for after current position to avoid double swaps for j in range(i+1, len(A)): if string[j] == A[i] and A[j] != A[i]: double_play = string[i] == A[j] change = -2 if double_play else -1 if k == best and distance % 2 == 1 and distance > 3 and not double_play: continue neighbor = swap(string, i, j) neighbors.append( (change, neighbor) ) neighbors.sort() total_count = 1 for (score, neighbor) in neighbors: found, count = DFS(neighbor, k-1, seen) total_count += count if found: return True, total_count return False, total_count for k in range(len(A), -1, -1): # for k in range(len(A)): found, count = DFS(B, k, set()) if not found: return k+1 return 0
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def neighbor(x): i = 0 while x[i] == B[i]: i += 1 for j in range(i+1, len(x)): if x[j] == B[i] and x[j] != B[j]: yield x[:i] + x[j] + x[i+1:j] + x[i] + x[j+1:] queue, seen = [(A, 0)], {A} for word, distance in queue: if word == B: return distance for neigh in neighbor(word): if neighbor not in seen: seen.add(neigh) queue.append((neigh, distance + 1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A, B): def find_neighbors(arr, i): e = A[i] nei = [] for x in range(i + 1, len(arr)): if arr[x] == e: nei.append(x) return nei q = deque() for i in range(len(A)): if A[i] == B[i]: continue else: q.append([list(B), i]) break l = 0 while q: for x in range(len(q)): e = q.popleft() y = e[1] while y < len(e[0]): #Move forward when elements of A and B are equal if e[0][y] == A[y]: y += 1 else: break if y == len(e[0]): #If everything is equal return level return l else: nei = find_neighbors(e[0], y) #Find all A[i]'s to the right of B which are prospective candidates for n in nei: new_e = e[0][:] new_e[n] = e[0][y] #Swap A[i] with all matched neighbors(Generating a new 'state') q.append([new_e, y + 1]) l += 1 return l
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A, B): out = 0 q = deque() for i in range(len(A)): if A[i] == B[i]: continue else: q.append([list(B), i]) break def find_neighbors(arr, i): e = A[i] nei = [] for x in range(i + 1, len(arr)): if arr[x] == e: nei.append(x) return nei while q: for _ in range(len(q)): cur, idx = q.popleft() while idx < len(cur): if cur[idx] == A[idx]: idx += 1 else: break if idx == len(cur): return out else: nei = find_neighbors(cur, idx) for n in nei: new = cur[:] new[n] = cur[idx] q.append([new, idx + 1]) out += 1 return out
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def bfs(self, A: str, B: str): if A == B: return 0 n = len(A) q = [] # visited = [False for i in range(n)] q.append((B, 0, 0)) while q: processed, idx, swaps = q.pop(0) while processed[idx] == A[idx]: idx += 1 for i in range(idx+1,n): if A[idx] == processed[i]: new_processed = processed[:idx] + processed[i] + processed[idx+1:i] + processed[idx] + processed[i+1:] if A == new_processed: return swaps + 1 q.append((new_processed, idx+1, swaps+1)) return 0 def kSimilarity(self, A: str, B: str) -> int: return self.bfs(A, B)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def check(a,i): if(a == B): return 0 if a[i] != B[i]: j=i minCount = None while(j<len(a)): if a[j] == B[i]: if a[i] == B[j]: return 1 + check(a[:i]+a[j]+a[i+1:j]+a[i]+a[j+1:],i+1) count = check(a[:i]+a[j]+a[i+1:j]+a[i]+a[j+1:],i+1) if minCount == None or minCount > count: minCount = count j+=1 return 1 + minCount else: return 0 + check(a,i+1) return check(A,0)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: A, B = list(A), list(B) res = float('inf') q = [(0, 0, B)] # (i) index, (k) swaps made, (B) current list while q: i, k, B = heapq.heappop(q) i = -i if k >= res: continue while (i < len(B)) and (B[i] == A[i]): i += 1 if i == len(B): res = min(res, k) continue target = A[i] j = i + 1 while j < len(B): if B[j] == target: heapq.heappush(q, (-(i+1), k+1, B[:j]+[B[i]]+B[j+1:])) j += 1 return res
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: visited, n = set(), len(A) q = collections.deque([(A, 0)]) while q: C, swaps = q.popleft() if C == B: return swaps visited.add(C) i = 0 while C[i] == B[i]: i += 1 for j in range(i + 1, n): if C[j] == B[i]: nxt = C[:i] + C[j] + C[i+1:j] + C[i] + C[j+1:] if nxt not in visited: q.append((nxt, swaps + 1)) return -1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def find_neighbors(arr, i): e = A[i] nei = [] for x in range(i + 1, len(arr)): if arr[x] == e: nei.append(x) return nei q = deque() for i in range(len(A)): if A[i] == B[i]: continue else: q.append([list(B), i]) break l = 0 while q: for x in range(len(q)): e = q.popleft() y = e[1] while y < len(e[0]): #Move forward when elements of A and B are equal if e[0][y] == A[y]: y += 1 else: break if y == len(e[0]): #If everything is equal return level return l else: nei = find_neighbors(e[0], y) #Find all A[i]'s to the right of B which are prospective candidates for n in nei: new_e = e[0][:] new_e[n] = e[0][y] #Swap A[i] with all matched neighbors(Generating a new 'state') q.append([new_e, y + 1]) l += 1 return l
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dist(a, b): # count number of differing characters return sum( (1 if c!=d else 0 for (c,d) in zip(a,b) ) ) def find_wrong_letters(working): # given a working string, return two dicts that track work to be done: # (wants, wrongs) # `wants[target]` maps to set of positions that need `target` letter # `wrongs[letter]` maps to set positions that have a wrong `letter` # if i in wants{target] and j in wrongs[letter] # we can swap ith and jth letters to improve working wants = defaultdict(set) wrongs = defaultdict(set) for i, c in enumerate(working): target = A[i] if c != target: wants[target].add(i) wrongs[c].add(i) return wants, wrongs def estimate_remaining(string): # count number of wrong letters # assume a perfect scenario: # each swap fixes two letters count = dist(string, A) return count/2 if count % 2 == 0 else count//2 + 1 def swap(s, i, j): # swap ith and jth chars of string s # assert i != j if i > j: i, j = j, i return s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] def extend(working): wrongs, needs = find_wrong_letters(working) for letter, wrong_set in list(wrongs.items()): return ( swap(working, i, j) for i in wrong_set for j in needs[letter] ) # print(A) # print(B) # print(\"0123456789abcdefghijklmnopqrst\"[:len(A)]) # needs, wrongs = find_wrong_letters(B) # print(f\"wrong: {wrongs}\") # print(f\"needs: {needs}\") # return 0 if A == B: return 0 # BFS # q is a heap that holds triples: # (estimate, swap_count_so_far, the_working_string) q = [ (0,0,B) ] seen = dict() best = len(A) while q: estimate, k, working = heapq.heappop(q) if estimate >= best or k >= best: return best if working == A: best = min( best, k) continue for extension in extend(working): if extension not in seen or seen[extension] > k+1: seen[extension] = k+1 new_estimate = estimate_remaining(extension) + k + 1 heapq.heappush( q, (new_estimate, k+1, extension) ) return best
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A, B): def find_neighbors(arr, i): e = A[i] nei = [] for x in range(i + 1, len(arr)): if arr[x] == e: nei.append(x) return nei q = deque() for i in range(len(A)): if A[i] == B[i]: continue else: q.append([list(B), i]) break l = 0 while q: for x in range(len(q)): e = q.popleft() y = e[1] while y < len(e[0]): #Move forward when elements of A and B are equal if e[0][y] == A[y]: y += 1 else: break if y == len(e[0]): #If everything is equal return level return l else: nei = find_neighbors(e[0], y) #Find all A[i]'s to the right of B which are prospective candidates for n in nei: new_e = e[0][:] new_e[n] = e[0][y] #Swap A[i] with all matched neighbors(Generating a new 'state') q.append([new_e, y + 1]) l += 1 return l
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 m = len(A) def getNeighbors(word): i = 0 while word[i] == B[i]: i+=1 for j in range(i+1, m): if word[j] == B[i]: yield word[:i] + word[j] + word[i+1:j] + word[i] + word[j+1:] visited = set([A]) queue = collections.deque([A]) k = 1 while queue: for _ in range(len(queue)): word = queue.popleft() for nbor in getNeighbors(word): if nbor not in visited: if nbor == B: return k queue.append(nbor) k += 1 raise Exception('invalid')
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 m = len(A) def getNeighbors(word): i = 0 while word[i] == B[i]: i+=1 for j in range(i+1, m): if word[j] == B[i] and word[j] != word[i]: yield word[:i] + word[j] + word[i+1:j] + word[i] + word[j+1:] visited = set([A]) queue = collections.deque([A]) k = 1 while queue: for _ in range(len(queue)): word = queue.popleft() for nbor in getNeighbors(word): if nbor not in visited: if nbor == B: return k queue.append(nbor) k += 1 raise Exception('invalid')
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: n = len(B) q = deque([B]) visited = {B} cnt = 0 while q: L = len(q) for _ in range(L): cur = q.popleft() if cur == A: return cnt pos = 0 while A[pos] == cur[pos]: pos += 1 curlist = list(cur) for i in range(pos+1, n): if curlist[i] == A[pos] and curlist[i] != A[i]: curlist[i], curlist[pos] = curlist[pos], curlist[i] curstr = ''.join(curlist) if curstr not in visited: q.append(curstr) visited.add(curstr) curlist[i], curlist[pos] = curlist[pos], curlist[i] cnt += 1 return cnt
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from functools import lru_cache class Solution: @lru_cache(None) def kSimilarity(self, A: str, B: str) -> int: if len(A)==0: return 0 if A[0]==B[0]: return self.kSimilarity(A[1:],B[1:]) for i in range(1,len(B)): if A[0]==B[i] and B[0]==A[i]: return 1 + self.kSimilarity(A[1:i]+A[i+1:], B[1:i]+B[i+1:]) ans = math.inf for i in range(1,len(B)): if A[0]==B[i]: ans = min(ans, 1+self.kSimilarity(A[1:], B[1:i]+B[0]+B[i+1:])) return ans
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 def swap(S, i, j): a = list(S) a[i], a[j] = a[j], a[i] return ''.join(a) n = len(A) steps = 0 q = deque() visited = set() q.append(A) visited.add(A) while q: steps += 1 sz = len(q) for _ in range(sz): S = q.popleft() i = 0 while S[i] == B[i]: i += 1 for j in range(i + 1, n): if S[j] != B[i] or S[j] == B[j]: continue T = swap(S, i, j) if T == B: return steps if T not in visited: q.append(T) visited.add(T) return steps
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: #assert(len(A) == len(B)) #assert(sorted(A) == sorted(B)) if A == B: return 0 seen = set(A) q = deque() q.append((A,0)) while q: currentString, k = q.popleft() for i in range(0, len(B)-1): if currentString[i] == B[i]: continue for j in range(i+1,len(B)): if currentString[j] == B[j]: continue if currentString[i] == currentString[j]: continue if currentString[j] != B[i]: continue newString = currentString[:i] + currentString[j] + currentString[i+1:j] + currentString[i] + currentString[j+1:] if newString == B: return k+1 if newString not in seen: q.append((newString,k+1)) seen.add(newString) break return -1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i] == B[i]: i+=1 #continue incremeneting if characters are same for j in range(i+1, len(x)): if x[j] == B[i] and x[j] != B[j]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] #swaps the two characters q, seen = [(A,0)], {A} while q != []: x, d = q.pop(0) if x == B: return d #return immediately from BFS if the strings are the same for y in nei(x): #print(\"y\", y) if y not in seen: seen.add(y), q.append((y,d+1)) #print(\"q\", q)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: if A==B: return 0 queue = deque([]) queue.append(A) step = 0 visited = set() while queue: length = len(queue) while length: length-=1 strings = queue.popleft() i = 0 while strings[i]==B[i]: i+=1 newstrings = list(strings) cha = strings[i] for j in range(i+1, len(strings)): if strings[j]==B[i] and strings[j]!=B[j]: newstrings[i], newstrings[j] = newstrings[j], newstrings[i] newstrings = ''.join(newstrings) if newstrings == B: return step+1 else: if newstrings not in visited: visited.add(newstrings) queue.append(newstrings) newstrings = list(strings) step+=1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: ''' A classic BFS solution to find the shortest path; The neighbors to each node string S are all the strings reachable with 1 swap, that match the first unmatched character in S. ''' def getNeighbors(s): for i, c in enumerate(s): if c!=B[i]: break s = list(s) for j in range(i+1,len(s)): if s[j]==B[i] and s[j]!=B[j]: s[i],s[j]=s[j],s[i] yield(''.join(s)) s[j],s[i]=s[i],s[j] queue = collections.deque([A]) seen = {A:0} while queue: s = queue.popleft() if s==B: return seen[s] for n in getNeighbors(s): if n not in seen: seen[n]=seen[s]+1 queue.append(n)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: l = len(A) def neighbor(s,pos,target): visited = set() t = list(s) for i in range(pos+1,l): if t[i] == target and (t[i],B[i]) not in visited: visited.add((t[i],B[i])) t[pos],t[i] = t[i],t[pos] yield(''.join(t)) t[pos],t[i] = t[i],t[pos] step = 0 index = 0 seen = set() q = collections.deque() q.append((A,0)) seen.add(A) while q: lq = len(q) for j in range(lq): tmp,k = q.popleft() if tmp == B: return step while k<l and tmp[k] == B[k]: k+=1 for n in neighbor(tmp,k,B[k]): if n not in seen: seen.add(n) q.append((n,k)) step += 1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 visited = set([A]) q = collections.deque([A]) res, n = 0, len(A) while q: res += 1 qn = len(q) for _ in range(qn): s = q.popleft() i = 0 while i < n and s[i] == B[i]: i += 1 for j in range(i+1, n): if s[j] == B[j] or s[j] != B[i]: continue tmp = self.swap(s, i, j) if tmp == B: return res if tmp not in visited: visited.add(tmp) q.append(tmp) return res def swap(self, s: str, i: int, j: int) -> str: l = list(s) l[i], l[j] = l[j], l[i] return ''.join(l)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dist(a, b): # count number of differing characters return sum( (1 if c!=d else 0 for (c,d) in zip(a,b) ) ) def find_wrong_letters(working): # given a working string, return two dicts that track work to be done: # (wants, wrongs) # `wants[target]` maps to set of positions that need `target` letter # `wrongs[letter]` maps to set positions that have a wrong `letter` # if i in wants{target] and j in wrongs[letter] # we can swap ith and jth letters to improve working wants = defaultdict(set) wrongs = defaultdict(set) for i, c in enumerate(working): target = A[i] if c != target: wants[target].add(i) wrongs[c].add(i) return wants, wrongs def swap(s, i, j): # swap ith and jth chars of string s # assert i != j if i > j: i, j = j, i return s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] def extend(working): wrongs, needs = find_wrong_letters(working) for letter, wrong_set in list(wrongs.items()): return ( swap(working, i, j) for i in wrong_set for j in needs[letter] ) # greedy BFS # only search # q holds triples: # (estimate, swap_count_so_far, the_working_string) q = deque() q.append( (0, 0, B) ) seen = dict() best = len(A) while q: estimate, k, working = q.popleft() if estimate >= best or k >= best: return best if working == A: best = min( best, k) continue improves_one, improves_two = [], [] for extension in extend(working): improvement = dist(A, working) - dist(A, extension) # improvement = 2 if improvement == 2: improves_two.append(extension) else: improves_one.append(extension) for extension in improves_two or improves_one: if extension not in seen or seen[extension] > k+1: seen[extension] = k+1 q.append( (0, k+1, extension) ) return best
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: # BFS # Key: at every step, we swap and make sure we put one char at correct pos, BFS makes sure it's shortest length = len(A) q = deque([A]) visited = set([A]) step = 0 while q: size = len(q) for _ in range(size): cur = q.popleft() if cur == B: return step for i in range(length): if cur[i] != B[i]: break for j in range(i + 1, length): # Key: NOT (cur[j] == B[i] or cur[i] == B[j])!!! Because in each BFS level we only need to make sure i pos can be corrected by swap(i,j), in the next level, since range of i and j is decreasing, width of BFS won't too large. # But cur[i]==B[j] NOT guarantee i pos is corrected, next level it might still starts from i, thus range of search is width and got TLE if cur[j] != B[j] and cur[j] == B[i]: new_str = cur[: i] + cur[j] + cur[i+1: j] + cur[i] + cur[j + 1:] if new_str not in visited: q.append(new_str) visited.add(new_str) step += 1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: @lru_cache(None) def kSimilarity(self, A: str, B: str) -> int: if len(A) == 0: return 0 if A[0]==B[0]: return self.kSimilarity(A[1:],B[1:]) for i in range(1,len(B)): if B[i]==A[0] and A[i]==B[0]: return 1 + self.kSimilarity(A[1:i]+A[i+1:],B[1:i]+B[i+1:]) ans = math.inf for i in range(1,len(B)): if B[i]==A[0]: ans = min(ans, 1+self.kSimilarity(A[1:],B[1:i]+B[0]+B[i+1:])) return ans
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def swapPos(string): i = 0 while string[i] == B[i]: i += 1 for j in range(i+1, length): if string[j] == B[i] and string[j] != B[j]: yield string[:i] + string[j] + string[i+1:j] + string[i] + string[j+1:] dq = deque([(A, 0)]) visited = set(A) length = len(A) while dq: string, dist = dq.popleft() if string == B: return dist for s2 in swapPos(string): if s2 not in visited: dq.append((s2, dist+1)) visited.add(s2)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
# class Solution: # def kSimilarity(self, A: str, B: str) -> int: # n = len(A) # A, B = list(A), list(B) # mp = defaultdict(list) # for i, ch in enumerate(A): # mp[ch].append(i) # def dfs(idx): # if idx == n: # return 0 # if A[idx] == B[idx]: # return dfs(idx+1) # res = float('inf') # for nxt in range(idx+1,n): # if A[nxt] != B[idx]: # continue # A[idx], A[nxt] = A[nxt], A[idx] # res = min(res, dfs(idx+1)+1) # A[idx], A[nxt] = A[nxt], A[idx] # return res # return dfs(0) from functools import lru_cache class Solution: def kSimilarity(self, A: str, B: str) -> int: n = len(A) # mp = defaultdict(list) # for i, ch in enumerate(A): # mp[ch].append(i) @lru_cache(None) def dfs(s): if s == B: return 0 idx = 0 while s[idx] == B[idx]: idx += 1 res = float('inf') for nxt in range(idx+1,n): if s[nxt] != B[idx] or s[nxt] == B[nxt]: continue res = min(res, dfs(s[:idx]+s[nxt]+s[idx+1:nxt]+s[idx]+s[nxt+1:])+1) return res return dfs(A)
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def next(s): for i in range(len(s)): if s[i] != B[i]: for j in range(i + 1,len(s)): if B[i] == s[j] and B[j] == s[i]: yield s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:] for j in range(i + 1,len(s)): if B[i] == s[j] and B[j] != s[j]: yield s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:] return res = 0 if A == B: return res q = [(A,0)] seen = {A} for s,i in q: if s == B: return i for n in next(s): if n not in seen: q.append((n,i + 1)) seen.add(n) return -1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def get_neis(s): res = [] for i in range(N): if s[i]!=B[i]: break temp = list(s) for j in range(i+1, N): if s[j]==B[i] and s[j]!=B[j]: temp[i], temp[j] = temp[j], temp[i] res.append(''.join(temp)) temp[i], temp[j] = temp[j], temp[i] return res if A==B: return 0 N = len(A) dq = deque([A]) seen = set([A]) cnt = 0 while dq: length = len(dq) for _ in range(length): s = dq.popleft() for nei in get_neis(s): if nei==B: return cnt+1 if nei not in seen: seen.add(nei) dq.append(nei) cnt += 1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def swap(s): i=0 while s[i]==B[i]: i+=1 for j in range(i+1,len(s)): if s[j]==B[i] and B[j]!=s[j]: yield s[:i]+s[j]+s[i+1:j]+s[i]+s[j+1:] q=collections.deque([(A,0)]) seen={A} while q: s,d=q.popleft() if s==B: return d for n in swap(s): if n not in seen: seen.add(n) q.append((n,d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i] == B[i]: i+=1 for j in range(i+1, len(x)): if x[j] == B[i] and x[j] != B[j]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] q, seen = [(A,0)], {A} for x, d in q: if x == B: return d for y in nei(x): if y not in seen: seen.add(y), q.append((y,d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: lsta, lstb = [], [] for i in range(len(A)): if A[i] != B[i]: lsta.append(A[i]) lstb.append(B[i]) A, B = ''.join(lsta), ''.join(lstb) q = deque([(0, A, B)]) while q: cost, curr, goal = q.popleft() if not curr: return cost need = goal[0] work = None for i in range(len(curr)): # need to find one where curr[i] == goal[0] and goal[i] == curr[0] if curr[i] == goal[0] and goal[i] == curr[0]: work = i break if work: # only swap this one # remove 0 and work from the unmatched list q.append((cost+1, curr[1:work]+curr[work+1:], goal[1:work]+goal[work+1:])) else: for i in range(1, len(curr)): if curr[i] == need: q.append((cost+1, curr[1:i]+curr[0]+curr[i+1:], goal[1:]))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i] == B[i]: i += 1 for j in range(i+1, len(x)): if x[j] == B[i] and x[j] != B[j]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] if not A or not B: return 0 queue, visited = [(A, 0)], {A} for x, d in queue: if x == B: return d for y in nei(x): if y not in visited: visited.add(y) queue.append((y, d+1))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A==B: return 0 dq = deque([(A,0)]) cnt = 0 N = len(A) seen = set() while dq: length = len(dq) for _ in range(length): curr, idx = dq.popleft() lst = list(curr) for i in range(idx, N): if lst[i]!=B[i]: for j in range(i+1, N): if lst[j]!=B[j] and lst[j]==B[i]: lst[i], lst[j] = lst[j], lst[i] temp = ''.join(lst) if temp==B: return cnt+1 if temp not in seen: seen.add(temp) dq.append((temp, i+1)) lst[i], lst[j] = lst[j], lst[i] break cnt += 1
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: if A == B: return 0 dp = [B] visit = {B} k = 0 while dp: k += 1 l = len(dp) for i in range(l): s = dp.pop() for j, c in enumerate(A): if c != s[j]: for n in range(j+1, len(A)): if s[n] == c: newS = s[0:j] + s[n] + s[j+1:n] + s[j] + s[n+1:] if newS not in visit: if newS == A: return k dp.insert(0, newS) visit.add(newS) break
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: def dist(a, b): # count number of differing characters return sum( (1 if c!=d else 0 for (c,d) in zip(a,b) ) ) def estimate_remaining(string): # count number of wrong letters # assume a perfect scenario: # each swap corrects two letters count = dist(string, A) return count/2 if count % 2 == 0 else count//2 + 1 def find_wrong_letters(working): # given a working string, return two dicts that track work to be done: # (wants, wrongs) # `wants[target]` maps to set of positions that need `target` letter # `wrongs[letter]` maps to set positions that have a wrong `letter` # if i in wants{target] and j in wrongs[letter] # we can swap ith and jth letters to improve working wants = defaultdict(set) wrongs = defaultdict(set) for i, c in enumerate(working): target = A[i] if c != target: wants[target].add(i) wrongs[c].add(i) return wants, wrongs def swap(s, i, j): # swap ith and jth chars of string s # assert i != j if i > j: i, j = j, i return s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:] def extend(working): wrongs, needs = find_wrong_letters(working) for letter, wrong_set in list(wrongs.items()): return ( swap(working, i, j) for i in wrong_set for j in needs[letter] ) # BFS # q is a heap that holds triples: # (estimate, swap_count_so_far, the_working_string) # q = deque() q = [ (0, 0, B) ] seen = dict() best = len(A) while q: estimate, k, working = heapq.heappop(q) if estimate >= best or -k >= best: return best if working == A: best = min( best, -k) continue for extension in extend(working): if extension not in seen or seen[extension] > -k+1: seen[extension] = k+1 new_estimate = estimate_remaining(extension) -k + 1 heapq.heappush( q, (new_estimate, k-1, extension) ) return best
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: # 根据最后一位是否相等进行贪心的求解。dfs深搜 memo = {} def helper(a, b): if a == b: return 0 if (a, b) in memo: return memo[(a, b)] res = float('inf') if a[-1] == b[-1]: res = min(res, helper(a[:-1], b[:-1])) else: for i in range(len(a) - 1): if a[i] == b[-1] and a[i] != b[i]: # 切记当前位置的ab不能对应相等,要不然肯定多操作了。 a_new = a[:i] + a[-1] + a[i + 1:-1] #把a[i]与a[-1]交换了 res = min(res, 1 + helper(a_new, b[:-1])) memo[(a, b)] = res return res return helper(A, B) ''' # 这道题虽然是hard但是通过你理解题意,很容易找到潜在的解答方法。 # 第一重思路就是看到最小。那么只要比最小的大都OK,那么很容易想到用二分法去猜测答案。但是如何check存疑 # 第二重思路就是看到两个node。第一个node通过交换变成第二个。交换的可能性很多。因此可以看乘bfs # BFS就是给你一个起点一个终点,问你最近的距离,最少的方式等等是多少! # by build directed edges bfs to find k def neighbor(x): i = 0 while x[i] == B[i]: # 更换的种类很多种,但是仍然需要剪枝无效的。因此相同部分就不改变了。 i += 1 for j in range(i + 1, len(x)): # 找到第一个不同的地方,然后去找可以过来填坑的letter if x[j] == B[i]: yield x[:i] + x[j] + x[i + 1:j] + x[i] + x[j + 1:] q, seen = [(A, 0)], {A} for x, d in q: if x == B: return d for y in neighbor(x): if y not in seen: seen.add(y) q.append((y, d + 1)) '''
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
class Solution: def kSimilarity(self, A: str, B: str) -> int: lsta, lstb = [], [] for i in range(len(A)): if A[i] != B[i]: lsta.append(A[i]) lstb.append(B[i]) A, B = ''.join(lsta), ''.join(lstb) q = deque([(0, A, B)]) while q: cost, curr, goal = q.popleft() if not curr: return cost need = goal[0] work = None for i in range(len(curr)): # need to find one where curr[i] == goal[0] and goal[i] == curr[0] if curr[i] == goal[0] and goal[i] == curr[0]: work = i break if work: # only swap this one q.append((cost+1, curr[1:work]+curr[work+1:], goal[1:work]+goal[work+1:])) else: for i in range(1, len(curr)): if curr[i] == need: q.append((cost+1, curr[1:i]+curr[0]+curr[i+1:], goal[1:]))
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that the resulting string equals B. Given two anagrams A and B, return the smallest K for which A and B are K-similar. Example 1: Input: A = "ab", B = "ba" Output: 1 Example 2: Input: A = "abc", B = "bca" Output: 2 Example 3: Input: A = "abac", B = "baca" Output: 2 Example 4: Input: A = "aabc", B = "abca" Output: 2 Note: 1 <= A.length == B.length <= 20 A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
from collections import deque class Solution: def kSimilarity(self, A: str, B: str) -> int: def nei(x): i = 0 while x[i] == B[i]: i+=1 for j in range(i+1, len(x)): if x[j] == B[i] and x[j] != B[j]: yield x[:i]+x[j]+x[i+1:j]+x[i]+x[j+1:] q, seen = [(A,0)], {A} for x, d in q: if x == B: return d for y in nei(x): if y not in seen: seen.add(y), q.append((y,d+1))