美文网首页
[LeetCode 87] Scramble String (H

[LeetCode 87] Scramble String (H

作者: 灰睛眼蓝 | 来源:发表于2019-06-17 15:58 被阅读0次

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 (H

    Given a string s1, we may represent it as a binary tree b...

  • 10.3 - hard总结2

    87. Scramble String: 区间dp加上memory search97. Interleaving ...

  • 2020-2-27 刷题记录

    今天,又只做了三道..... 0X00 leetcode 做了 3 道 leetcode 87. Scramble...

  • 87.Scramble String

    https://leetcode.com/problems/scramble-string/ 题意不是简单的rev...

  • 87. Scramble String

    具体链接戳这千万不要以为它是均等分!!!!!!解题思路: 如果两个字符串的各个字符数量都不相等,直接返回false...

  • 87. Scramble String

    Given a string s1, we may represent it as a binary tree b...

  • 87. Scramble String

    问题描述 Given two strings s1 and s2 of the same length, dete...

  • Scramble String

    题目Given a string s1, we may represent it as a binary tree...

  • 087 Scramble String

    Given a string s1, we may represent it as a binary tree b...

  • Scramble String(未完待续)

    题目链接 题意 见题目链接 解答 因为二叉树是由字符串递归地分割生成的,所以能很自然地想到递归的解法。遍历 s1 ...

网友评论

      本文标题:[LeetCode 87] Scramble String (H

      本文链接:https://www.haomeiwen.com/subject/xugafctx.html