class Solution(object): def isScramble(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ if len(s1)!=len(s2): return False if s1==s2: return True l1=list(s1) l2=list(s2) l1.sort() l2.sort() if l1!=l2: return False length=len(s1) for i in range(1,length): if self.isScramble(s1[:i],s2[:i]) and self.isScramble(s1[i:],s2[i:]): return True if self.isScramble(s1[:i],s2[length-i:]) and self.isScramble(s1[i:],s2[:length-i]): return True return False
class Solution(object): def isScramble(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ if len(s1) != len(s2): return False dp = [[[0 for _ in range(len(s1))] for _ in range(len(s1))] for _ in range(len(s1))] for i in range(len(s1)): for j in range(len(s1)): if s1[i] == s2[j]: dp[0][i][j] = 1 for k in range(len(s1)): for i in range(len(s1)-k): for j in range(len(s1)-k): if dp[k][i][j] == 0: for q in range(k): if (dp[q][i][j] and dp[k-q-1][i+q+1][j+q+1]) or (dp[q][i][j+k-q] and dp[k-q-1][i+q+1][j]): dp[k][i][j] = 1 # print dp if dp[-1][0][0] == 1: return True else: return False