Wednesday, May 21, 2014

LeetCode: Scramble String

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.


As the problem says, the binary tree is constructed recursively. So does this problem, the left part of the string can be swapped or not. If it has been swapped, it will be mapped to the right part of s2. And left part of s2 will be mapped to right part of s1.

C++ Code:

/* func: is_scrambled
 * goal: to check if s2 is a scrambled string of s1
 * @param s1: string s1
 * @param s2: string s2
 * return: true or false
 */
bool is_scrambled(string s1, string s2){
    if(s1.lenght() != s2.length()){
        return false;
    }
    int A[26];
    memset(A,0,26*sizeof(A[0]));
    //To check if current s1 and s2 have the same char set
    for(int i =0;i<s1.size(); i++)
    {
        A[s1[i]-'a']++;
    }
    for(int i =0;i<s2.size(); i++)
    {
        A[s2[i]-'a']--;
    }
    for(int i =0;i<26; i++)
    {
        if(A[i] !=0)
            return false;
    }
    if(s1.size() ==1 && s2.size() ==1) return true;
    
    for(int i=1; i<s1.length(); i++){
        //Check if scrambled recursively
        if(is_scrambled(s1.substr(0, i), s2.substr(s1.length()-i)) && is_scrambled(s1.substr(i), s2.substr(0, s1.length()-i)))
            return true;
        if(is_scrambled(s1.substr(0, i), s2.substr(0, i)) && is_scrambled(s1.substr(i), s2.substr(i)))
           return true;
    }
    return false;
}

Python Code:

# func: check if one string is a scramble string of the other one
# @param s1: string s1
# @param s2: string s2
# @return: True or False
def is_scrambled(s1, s2):
    if len(s1) != len(s2):
        return False
    char_dict = {}
    for char in s1:
        if char in char_dict:
            char_dict[char] += 1
        else:
            char_dict[char] = 1
    for char in s2:
        if char not in char_dict:
            return False
        else:
            char_dict[char] -= 1

    for freq in char_dict.values():
        if freq != 0:
            return False

    if len(s1) == len(s2) == 1:
        return True

    for i in xrange(1, len(s1)):
        if is_scrambled(s1[:i], s2[:i]) and is_scrambled(s1[i:], s2[i:]):
            return True

        if is_scrambled(s1[:i], s2[-i:]) and is_scrambled(s1[i:], s2[:-i]):
            return True
    return False

No comments:

Post a Comment