2014年10月12日星期日

[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.
DFS题目,对于两个字符串,如果它们都是空的,则返回true,这个可以算作是一个corner case,之后如果它们都是只有一个字符的,那么直接比较返回。对于长度大于1的字符串,如s1和s2,可以做如下的比较:

  1. s1分割成s11和s12,s2在同样位置分割s21,s22,然后分别递归s11,s21与s12,s22
  2. s1分割成s11和s12,s2从尾部往前分割成对应大小的s21,s22,然后分别递归s11,s22和s12,s21
在比较的时候,可以用一个26位的数组来进行快速的判断两个字符串字母个数是否相同从而提前终止非法情况。

    public boolean isScramble(String s1, String s2) {
        if(s1 == null && s2 == null) return true; // corner case
        if(s1.length() != s2.length()) return false;
        if(s1.length() == 1 && s1.equals(s2)) return true; // base case, only one element
        
        /*char[] ch1 = s1.toCharArray();
        char[] ch2 = s2.toCharArray();
        Arrays.sort(ch1); Arrays.sort(ch2);
        for(int i = 0; i < ch1.length; i++) {
            if(ch1[i] != ch2[i]) return false;
        }*/
        int[] num1 = new int[26];
        int[] num2 = new int[26];
        for(int i = 0; i < s1.length(); i++) {
            num1[s1.charAt(i) - 'a']++;
            num2[s2.charAt(i) - 'a']++;
        }
        for(int i = 0;i < 26; i++) {
            if(num1[i] != num2[i]) return false;
        }
        for(int i = 1; i <= s1.length() - 1; i++) {
            if((isScramble(s1.substring(0, i), s2.substring(0, i)) && isScramble(s1.substring(i), s2.substring(i))) ||
            (isScramble(s1.substring(0, i), s2.substring(s2.length() - i)) && isScramble(s1.substring(i), s2.substring(0, s2.length() - i)))) return true;
        }
        return false;
    }

今天看了能用DP来求解,就用DP又写了一次,需要一个三维DP来存储足够的信息,dp[len][i][j]表示以s1以i开始和s2以j开始长度为len的子串是否是scramble string,状态转移方程为:

dp[len][i][j] = true if exist k so that dp[k][i][j] = true && dp[len][i+k][j+k] = true or
dp[k][i][j+len-k] = true && dp[len-k][i+k][j] = true

最终的代码虽然是套了4层循环,但是平摊分析之后复杂度应该还是O(n^3)级别的。
    public boolean isScramble(String s1, String s2) {
        int n = s1.length();
        boolean[][][] dp = new boolean[n+1][n][n];
        for(int len = 1; len <= n; len++) {
            for(int i = 0; i <= n-len; i++) {
                for(int j = 0; j <= n-len; j++) {
                    if(len == 1) {
                        if(s1.charAt(i) == s2.charAt(j)) 
                            dp[len][i][j] = true;
                        else 
                            dp[len][i][j] = false;
                    }
                    else {
                     dp[len][i][j] = false;
                        for(int k = 1; k < len; k++) {
                            if((dp[k][i][j] == true && dp[len-k][i+k][j+k] == true) || (dp[k][i][j+len-k] == true && dp[len-k][i+k][j] == true)) {
                                dp[len][i][j] = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return dp[n][0][0];
    }

没有评论:

发表评论