[LeetCode 87] Scramble String (Hard)

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.

Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true

Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false

Solution: Recursively get if left and right of two strings can match

  1. 题目说To scramble the string, we may choose any non-leaf node and swap its two children.
    可以发现如果:如下例题,两者互为scramble String
    • String1left 可以match String2left。同时,String1right 可以match String2right。比如greateat子字符串,和 rgeateat子字符串.
    • 或者String1left 可以match String2right。同时,String1right 可以match String2left。比如greatgr子字符串,和 rgeatrg子字符串。此时需注意String1leftString2right的长度需匹配!!!
  2. base Case:
    -- 两个字符串长度不相等,return false
    -- 当前两个字符长度为1,且相等 --> return true, 长度为1,但不相等 --> return false
    -- 两个字符串有不同的字符集, --> return false
  3. Recursively 判断两个字符串的左右子串是否匹配。一旦找到即 --> return true。如果全部找完都没有找到,则return false
image.png
class Solution {
    public boolean isScramble(String s1, String s2) {
        //base case 1
        if (s1 == null || s2 == null || s1.length () != s2.length ())
            return false;
        
        //base case 2 (recurvisly devide to only one character)
        if (s1.length () == 1) {
            if (s1.equals(s2))
                return true;
            else
                return false;
        }
        
        // base case 3
        // if s1 and s2 has different characters, they are not scramble string!!!
        char[] s1List = s1.toCharArray ();
        char[] s2List = s2.toCharArray ();
        Arrays.sort (s1List);
        Arrays.sort (s2List);
        
        for (int i = 0; i < s1List.length; i++) {
            if (s1List[i] != s2List[i]) {
                // System.out.println (Arrays.toString (s1List));
                // System.out.println (Arrays.toString (s2List));
                // System.out.println ("1!!!!");
                return false;
            }
                
        }
        
        // if it's not one character string, continue recursilvely handling
        for (int index = 1; index < s1.length (); index ++) {
            String s1Left = s1.substring (0, index);
            String s1Right = s1.substring (index, s1.length ());
            
            String s2Left = s2.substring (0, index);
            String s2Right = s2.substring (index, s2.length ());
            
            String s2Right2 = s2.substring (s2.length () - index, s2.length ());
            String s2Left2 = s2.substring (0, s2.length () - index);
            
            if ((isScramble (s1Left, s2Left) && isScramble (s1Right, s2Right)) ||
                (isScramble (s1Left, s2Right2) && isScramble (s1Right, s2Left2))) {
                return true;
            }
        }
        
        return false;
    }
}

你可能感兴趣的:([LeetCode 87] Scramble String (Hard))