#87 Scramble String

Description

We can scramble a string s to get a string t using the following algorithm:

  • If the length of the string is 1, stop.
  • If the length of the string is > 1, do the following:
  • Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
  • Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
  • Apply step 1 recursively on each of the two substrings x and y.
  • Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.

Examples

Example 1:

Input: s1 = “great”, s2 = “rgeat”
Output: true
Explanation: One possible scenario applied on s1 is:
“great” --> “gr/eat” // divide at random index.
“gr/eat” --> “gr/eat” // random decision is not to swap the two substrings and keep them in order.
“gr/eat” --> “g/r / e/at” // apply the same algorithm recursively on both substrings. divide at ranom index each of them.
“g/r / e/at” --> “r/g / e/at” // random decision was to swap the first substring and to keep the second substring in the same order.
“r/g / e/at” --> “r/g / e/ a/t” // again apply the algorithm recursively, divide “at” to “a/t”.
“r/g / e/ a/t” --> “r/g / e/ a/t” // random decision is to keep both substrings in the same order.
The algorithm stops now and the result string is “rgeat” which is s2.
As there is one possible scenario that led s1 to be scrambled to s2, we return true.

Example 2:

Input: s1 = “abcde”, s2 = “caebd”
Output: false

Example 3:

Input: s1 = “a”, s2 = “a”
Output: true

Constraints:

s1.length == s2.length
1 <= s1.length <= 30
s1 and s2 consist of lower-case English letters.

思路

首先明确一个点
要判断两个字符串是否Scramble,最简单的方式就是循环(切割位置)判断(substring能否Scramble)。

也就是说,对于s1 和 s2,假设他们长度相同且为n,则需要循环n - 1次,每次循环执行以下操作

以第k次操作为例

  • s1切分为s1_1 = s1[0, k],s1_2 = s1[k + 1, n - 1]
  • s2切分为s2_1 = s2[0, k],s2_2 = s2[k + 1, n - 1]
  • 检测是否满足 s1_1 和 s2_1 Scramble 且 s1_2 和 s2_2 Scramble
  • 重构s2 = s2_2 + s2_1,重新对s2进行切分
  • 检测是否满足 s1_1 和 s2_1 Scramble 且 s1_2 和 s2_2 Scramble
  • 只要满足以上任意一个条件,则证明s1和s2是Scramble的

按照以上的操作不难想到暴力操作的解法,但是,与字符串相关的操作,大多可以使用动态规划的方式进行求解

所以根据以上的步骤,能不能构建出状态转移矩阵呢?
可以看到,对于s1和s2两个同样长为n的字符串,他们是否scramble的状态取决于循环切分中是否存在子串Scramble的情况

换句话来说,我们只要对长度为k的s1/s2子串进行Scramble判断之后,就可以使用他们的状态对长度为k + 1的子串进行Scramble判断

这样的话就可以用一个三维矩阵来存储这个状态转移矩阵dp[i][j][k]

  • i i i表示s1中子串起始位置
  • j j j表示s2中子串起始位置
  • k k k表示子串长度

dp[i][j][k] 就表示 s1[i, i + k], s2[j, j + k] 是否Scramble

对于矩阵初始化,dp[i][j][1] 通过 s1[i] == s2[j] 进行

然后以 k k k为最外层循环,以 < k 的状态更新 k 长度子串的状态

最后输出 dp[0][0][n]

代码

class Solution {
    public boolean isScramble(String s1, String s2) {
        int n = s1.length();
        boolean[][][] dp = new boolean[n][n][n + 1];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                dp[i][j][1] = (s1.charAt(i) == s2.charAt(j));
            }
        }
        for(int k = 2; k <= n; k++){
            for(int i = 0; i <= n - k; i++){
                for(int j = 0; j <= n - k; j++){
                    dp[i][j][k] = false;
                    for(int p = 1; p < k; p++){
                        if((dp[i][j][p] && dp[i + p][j + p][k - p]) || (dp[i][j + k - p][p] && dp[i + p][j][k - p])){
                            dp[i][j][k] = true;
                            break;
                        }
                    }
                    
                }
            }
        }
        return dp[0][0][n];
    }
}

其他

为什么网上会有一堆暴力求解的方法!!!!!!!!!
明明不管怎么优化都不可能通过
附一下暴力的两种代码(加了预先判断也没用的!)

class Solution {
    String pattern;
    boolean answer;
    public void Scramble(String s, int start, int end){
        if(answer)
            return;
        if(start == end){
            answer = pattern.equals(s);
            return;
        }
        for(int i = start + 1; i < end + 1; i++){
            String s1 = s;
            String s2 = s1.substring(0, start) + s1.substring(i, end + 1) + s1.substring(start, i);
            if(end + 1 != s.length())
                s2 = s2 + s1.substring(end + 1, s.length());
            if(s1.equals(pattern) || s2.equals(pattern))
                answer = true;
            Scramble(s1, start, i - 1);
            Scramble(s1, i, end);
            Scramble(s2, start, start + (end - i));
            Scramble(s2, start + (end - i) + 1, end);
        }
    }
    
    public boolean isScramble(String s1, String s2) {
        pattern = s2;
        answer = false;
        Scramble(s1, 0, s2.length() - 1);
        return answer;
    }
}
class Solution {
    public boolean isScramble(String s1, String s2) {
        if(s1.equals(s2))
            return true;
        
        for(int i = 1; i < s1.length(); i++){
            String s1_1 = s1.substring(0, i);
            String s1_2 = s1.substring(i);
            String s2_1 = s2.substring(0, i);
            String s2_2 = s2.substring(i);
            if(isScramble(s1_1, s2_1) && isScramble(s1_2, s2_2))
                return true;
            s2_1 = s2.substring(s2.length() - i);
            s2_2 = s2.substring(0, s2.length() - i);
            if(isScramble(s1_1, s2_1) && isScramble(s1_2, s2_2))
                return true;
        }
        return false;
    }
}
class Solution {
    public boolean isScramble(String s1, String s2) {
         if(s1.length() != s2.length())  
              return false;  
         
         if(s1.length()==1 && s2.length()==1)
              return s1.charAt(0) == s2.charAt(0);  
     
        char[] t1 = s1.toCharArray(), t2 = s2.toCharArray();
        Arrays.sort(t1);
        Arrays.sort(t2);
        if(!new String(t1).equals(new String(t2)))
          return false;
          
        if(s1.equals(s2)) 
         return true;
         
        for(int split = 1; split < s1.length(); split++){
            String s11 = s1.substring(0, split);
            String s12 = s1.substring(split);
            
            String s21 = s2.substring(0, split);
            String s22 = s2.substring(split);
            if(isScramble(s11, s21) && isScramble(s12, s22))
              return true;
            
            s21 = s2.substring(0, s2.length() - split);
            s22 = s2.substring(s2.length() - split);
            if(isScramble(s11, s22) && isScramble(s12, s21))
             return true;
        }
        return false;
    }
}

动态规划甚至是最短的呢()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值