Accepted Python Solution

  • 0
    from collections import Counter
    class Solution(object):
        def isScramble(self, s1, s2):
            :type s1: str
            :type s2: str
            :rtype: bool
            # Base Case
            if s1 is None or s2 is None or len(s1) <= 1 or len(s2) <= 1:
                return s1 == s2
            elif len(s1) != len(s2):
                return False
            elif s1 == s2:
                return True
            # Character Count Must Equal
            if Counter(s1) != Counter(s2):
                return False
            # Recursive Part
            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      

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.