Saturday, July 18, 2015

Scramble String

Scramble String

29%
Accepted
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 ofs1 = "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.
Have you met this question in a real interview? 
Yes
Example
Challenge
O(n3) time

public class Solution {
    /**
     * @param s1 A string
     * @param s2 Another string
     * @return whether s2 is a scrambled string of s1
     */
    public boolean isScramble(String s1, String s2) {
        // Write your code here
        if(s1 == null || s2 == null || s1.length() != s2.length()) return false;
        char[] arr1 = s1.toCharArray();
        char[] arr2 = s2.toCharArray();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        if(!new String(arr1).equals(new String(arr2))) return false;
        else if(arr1.length == 1) return true;
        
        for(int i = 0; i < s1.length()-1; i++){
            if(isScramble(s1.substring(0, i+1), s1.substring(0, i+1)) && isScramble(s1.substring(i+1), s2.substring(i+1)) || 
            isScramble(s1.substring(0, i+1), s2.substring(s2.length() - i - 1)) && isScramble(s1.substring(i+1), s2.substring(0, s2.length() - i - 1)))
            return true;
        }
        return false;
    }
}

public class Solution {
    /**
     * @param s1 A string
     * @param s2 Another string
     * @return whether s2 is a scrambled string of s1
     */
    public boolean isScramble(String s1, String s2) {
        // Write your code here
        if(s1 == null || s2 == null || s1.length() != s2.length()) return false;
        int n = s1.length();
        boolean [][][] dp = new boolean[n][n][n+1];
        for(int i = n - 1; i >= 0; i--){
            for(int j = n - 1; j >= 0; j--){
                for(int k = 1; k <= n - Math.max(j, i); k++){
                    if(s1.substring(i, k+i).equals(s2.substring(j, j+k)))
                       dp[i][j][k] = true;
                    else {
                        for(int l = 1; l < k; l++){
                            if(dp[i][j][l] && dp[i+l][j+l][k-l] || dp[i][j+k-l][l] && dp[i+l][j][k-l]){
                                dp[i][j][k] = true;
                                break;
                            }
                        }
                    }    
                }
            }
        }
        return dp[0][0][n];
    }

}


No comments:

Post a Comment