Hard-题目17:87. Scramble String

原创 2016年05月31日 23:32:30

题目原文:
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”.
题目大意:
给出Scramble string的定义是,把s1从其中某处拆开,两边组成一棵二叉树的左子树和右子树,再递归进行下去,直到只有一个字符,这样组成了一棵二叉树,其中叶子节点的“轮廓”依次是字符串中的每个字符。如果存在一种二叉树,把其中的一个节点的左右孩子调换,得到的“轮廓”是s2,则称s1和s2是scramble。
给出两个字符串s1和s2,判断s1和s2是不是一对scramble string.
题目分析:
粗看起来很难,其实可以s1和s2是scramble string只有以下几种情况:
(1) s1==s2.
(2) 存在i,使得s1的前i个字符和s2的前i个字符是scramble,后半部分也是scramble。(相当于左子树和右子树不用调换)
(3) 存在i,使得s1的前i个字符和s2的后i个字符是scramble,s1的后半部分和s2的前半部分是scramble。(相当于从第i个点处把s1拆开,再交换左右子树。)
接下来就是递归处理了。需要做一个剪枝处理,就是当s1和s2中的字符不能一一对应时,则一定不是scramble的。
源码:(language:java)

public class Solution {
    public boolean isScramble(String s1, String s2) {
        int len1 = s1.length(),len2 = s2.length();
        if(len1 != len2)
            return false;
        else if(len1==1)
            return s1.charAt(0)==s2.charAt(0);
        else if(s1.equals(s2))
            return true;
        else {
            char[] ca1 = s1.toCharArray(),ca2 = s2.toCharArray();
            Arrays.sort(ca1);
            Arrays.sort(ca2);
            for(int i = 0;i<len1;i++)
                if(ca1[i]!=ca2[i])
                    return false;
            boolean result = false;
            for(int i = 1;i<len1;i++) {
                String s11 = s1.substring(0, i);
                String s12 = s1.substring(i);
                String s21 = s2.substring(0, i);
                String s22 = s2.substring(i);
                String s23 = s2.substring(0,len2-i);
                String s24 = s2.substring(len2-i);
                if(isScramble(s11,s21)&&isScramble(s12,s22) || (isScramble(s11,s24)&&isScramble(s12,s23))) {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
}

成绩:
7ms,beats 54.91%,众数4ms,21.84%
Cmershen的碎碎念:
这道题还可以用dp来消除递归,我一开始尝试的是记忆化搜索,但没想到Java中有什么合适的数据结构来记录搜索过的字符串。还有这个字符串的切分很适合用python写(比如用[i:],[:i]等很简洁的表示法),但我python写的太少了……语法不太熟练……

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

LeetCode 87. Scramble String(字符串扰乱)

原题网址:https://leetcode.com/problems/scramble-string/ Given a string s1, we may represent it as a b...
  • jmspan
  • jmspan
  • 2016年05月23日 00:34
  • 487

LeetCode --- 87. Scramble String

题目链接:Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to ...
  • makuiyu
  • makuiyu
  • 2015年04月07日 21:52
  • 1645

【LeetCode】87. Scramble String解法及注释

87. Scramble String Total Accepted: 44881 Total Submissions: 170059 Difficulty: Hard Given a strin...
  • Jin_Kwok
  • Jin_Kwok
  • 2016年04月12日 19:48
  • 939

Leetcode 87. Scramble String

87. Scramble String Total Accepted: 43512 Total Submissions: 166202 Difficulty: Hard Given a ...
  • fantasiasango
  • fantasiasango
  • 2016年03月14日 01:31
  • 282

leetCode 87. Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin...
  • liujunzxcv
  • liujunzxcv
  • 2017年09月26日 17:48
  • 43

LeetCode||87. Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin...
  • Lingfu74
  • Lingfu74
  • 2018年01月15日 16:18
  • 29

Leetcode 87. Scramble String

问题描述 Given a string s1, we may represent it as a binary tree by partitioning it to two non-...
  • u010370157
  • u010370157
  • 2017年07月12日 10:57
  • 75

***Leetcode 87. Scramble String

scramble string
  • ruihuank
  • ruihuank
  • 2017年12月20日 16:35
  • 30

[leetcode]87. Scramble String

题目链接:https://leetcode.com/problems/scramble-string/description/ Given a string s1, we may r...
  • xiaocong1990
  • xiaocong1990
  • 2017年08月28日 21:43
  • 144

[LeetCode]87. Scramble String

https://leetcode.com/problems/scramble-string/ 判断两个等长的字符串是否可以由另一个经过旋转得到 两个剪枝:1、已经判定为旋转对的做c...
  • gqk289
  • gqk289
  • 2017年01月25日 00:18
  • 78
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目17:87. Scramble String
举报原因:
原因补充:

(最多只允许输入30个字)