leetcode 87scamblestring

来源:互联网 发布:软件开发工程师前景 编辑:程序博客网 时间:2024/05/18 02:52
class Solution(object):
    def isScramble(self, s1, s2):
        """
        :type s1: str
        :type s2: str
        :rtype: bool
        """
        n, m = len(s1), len(s2)
        if n != m or sorted(s1) != sorted(s2):
            return False
        if n < 4 or s1 == s2:
            return True
        f = self.isScramble
        for i in range(1, n):
            if f(s1[:i], s2[:i]) and f(s1[i:], s2[i:]) or \
                f(s1[:i], s2[-i:]) and f(s1[i:], s2[:-i]):
                return True
        return False
0 0