Leetcode: Scramble String

Question

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = “great”:

great

/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node “gr” and swap its two children, it produces a scrambled string “rgeat”.

rgeat

/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that “rgeat” is a scrambled string of “great”.

Similarly, if we continue to swap the children of nodes “eat” and “at”, it produces a scrambled string “rgtae”.

rgtae

/ \
rg tae
/ \ / \
r g ta e
/ \
t a
We say that “rgtae” is a scrambled string of “great”.

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

Solution 1 (Wrong)

Code

 class Solution(object):
    def isScramble(self, s1, s2):
        """ :type s1: str :type s2: str :rtype: bool """

        s1,s2 = list(s1), list(s2)
        return self.helper(s1, 0, len(s1)-1, s2, 0, len(s2)-1)

    def helper(self, s1, a1, b1, s2, a2, b2):
        if b1-a1 != b2-a2:
            return False

        if a1==b1:
            return s1[a1]==s2[a2]


        for ind in range(a1,b1):
            # left-left, right-right
            if self.equals(s1[a1:ind+1], s2[a2:a2+ind-a1+1]) and self.equals(s1[ind+1:b1+1], s2[b2-(b1-ind-1):b2+1]):
                return True
            # left-right, right-left
            if self.equals(s1[a1:ind+1], s2[b2-(ind-a1):b2+1]) and self.equals(s1[ind+1:b1+1], s2[a2:a2+b1-ind]):
                return True

        return False


    def equals(self, s1, s2):
        """ to determine whether s1, s2 contain the same chars. """
        dictin={}
        for elem in s1:
            if elem not in dictin.keys():
                dictin[elem] = 1
            else:
                dictin[elem] += 1
        for elem in s2:
            if elem not in dictin.keys():
                dictin[elem] = -1
            else:
                dictin[elem] -= 1

        for value in dictin.values():
            if value!=0:
                return False

        return True

Analysis

My fault is that even s1, s2 have the same chars, they are not scrambled. For instance, s1=”abcd”, s2=”bdac”.

Solution2

Analysis

Get idea from here.

Code

class Solution(object):
    def isScramble(self, s1, s2):
        """ :type s1: str :type s2: str :rtype: bool """

        s1,s2 = list(s1), list(s2)
        return self.helper(s1, 0, len(s1)-1, s2, 0, len(s2)-1)

    def helper(self, s1, a1, b1, s2, a2, b2):
        if b1-a1 != b2-a2:
            return False

        if a1==b1:
            return s1[a1]==s2[a2]

        if self.equals(s1[a1:b1+1], s2[a2:b2+1])==False:
            return False


        for ind in range(a1,b1):
            # left-left, right-right
            if self.helper(s1,a1,ind, s2,a2,a2+ind-a1)   and self.helper(s1,ind+1,b1, s2,b2-(b1-ind-1),b2):
                return True
            # left-right, right-left
            if self.helper(s1,a1,ind, s2,b2-(ind-a1),b2) and self.helper(s1,ind+1,b1, s2,a2,a2+b1-ind-1):
                return True

        return False


    def equals(self, s1, s2):
        """ to determine whether s1, s2 contain the same chars. """
        dictin={}
        for elem in s1:
            if elem not in dictin.keys():
                dictin[elem] = 1
            else:
                dictin[elem] += 1
        for elem in s2:
            if elem not in dictin.keys():
                dictin[elem] = -1
            else:
                dictin[elem] -= 1

        for value in dictin.values():
            if value!=0:
                return False

        return True

Solution3

dynamic programming by using three dimension array

Get idea from here.

你可能感兴趣的:(Leetcode: Scramble String)