087 Scramble String

087 Scramble String

这道题是divide and conquer。 使用sorted可以快速判断 要不然会超时

class Solution:
    # @param {string} s1
    # @param {string} s2
    # @return {boolean}
    def isScramble(self, s1, s2):
        if s1 == s2:
            return True
        if sorted(s1) != sorted(s2):
            return False
        for i in range(1, len(s1)):
            if self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:],s2[i:]):
                return True
            if self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:],s2[:-i]):
                return True
        return False

 

posted @ 2015-08-01 11:48  dapanshe  阅读(87)  评论(0编辑  收藏  举报