[LeetCode] Scramble String

本文深入探讨了使用深度优先搜索(DFS)和回溯算法解决字符串乱序问题的技巧,通过递归地分解问题为子问题来判断两个字符串是否互为乱序形式。同时,文章涉及三维动态规划的应用,展示了复杂问题简化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一题一句:经典的DFS+BackTracking, tree的结构就是左右,任何俩个string是不是scramble string 完全取决于他们分别的子node对应是不是scramble string 或者 交叉对应是不是scramble string(左左,右右 和 左右,右左), 交叉对应的情况有点绕要留意。

补充, 这个题还是经典的3D dynamic 题目。

public class Solution {
    public boolean isScramble(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        return rec(s1,s2);
    }
    
    public boolean rec(String s1, String s2){
        int l= s1.length();
        if(l== 1){
            return s1.equals(s2);
        }
        if(!worthTrying(s1,s2)){
            return false;
        }
        //This is necessary.
        if(s1.equals(s2))
            return true;
        //The point is, don't let i = 0. Recursively solve subproblems, itself is not a subproblem.
        //And we (already) solve itself in current loop.
        for(int i=1;i
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值