当前位置:网站首页>87. 扰乱字符串
87. 扰乱字符串
2022-07-26 05:10:00 【小卢要刷力扣题】
前言
使用下面描述的算法可以扰乱字符串 s 得到字符串 t :
如果字符串的长度为 1 ,算法停止
如果字符串的长度 > 1 ,执行下述步骤:
在一个随机下标处将字符串分割成两个非空的子字符串。即,如果已知字符串 s ,则可以将其分成两个子字符串 x 和 y ,且满足 s = x + y 。
随机 决定是要「交换两个子字符串」还是要「保持这两个子字符串的顺序不变」。即,在执行这一步骤之后,s 可能是 s = x + y 或者 s = y + x 。
在 x 和 y 这两个子字符串上继续从步骤 1 开始递归执行此算法。
给你两个 长度相等 的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 true ;否则,返回 false 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/scramble-string
暴力递归
样本对应模型
按第一刀的情况来分
定义函数f(s1,L1,R1, s2,L2,R2):
str1的L1-R1,对str2 L2-R2等长,看str1的L1~R1,对str2 L2~R2这一段是不是玄变字符串
base case
当你L1到R1上只有一个字符,
如果str1[L1]==str2[L2],返回true
普遍情况
枚举的方式是str1第一刀切哪儿了
1)第一刀0-0, 1-9

两种情况
1.两个字符串一一对应
2.str1前部分对于str2后部分,str1后部分对应str2前部分
即字符进行了变换
2)0-1,2-9
…
9)0-8,9-9
public static boolean isScramble(String s1, String s2) {
if ((s1 == null && s2 != null) || (s1 != null && s2 == null)) {
return false;
}
if (s1 == null && s2 == null) {
return true;
}
if (s1.equals(s2)) {
return true;
}
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
if (!sameTypeSameNumber(str1, str2)) {
return false;
}
return process0(str1, 0, str1.length - 1, str2, 0, str2.length - 1);
}
// str1[L1...R1] str2[L2...R2] 是否互为玄变串
// 一定保证这两段是等长的!
public static boolean process(char[] str1, int L1, int R1, char[] str2, int L2, int R2) {
if (L1 == R1) {
return str1[L1] == str2[L2];
}
for (int leftEnd = L1; leftEnd < R1; leftEnd++) {
boolean p1 = process0(str1, L1, leftEnd, str2, L2, L2 + leftEnd - L1)
&& process0(str1, leftEnd + 1, R1, str2, L2 + leftEnd - L1 + 1, R2);
boolean p2 = process0(str1, L1, leftEnd, str2, R2 - (leftEnd - L1), R2)
&& process0(str1, leftEnd + 1, R1, str2, L2, R2 - (leftEnd - L1) - 1);
if (p1 || p2) {
return true;
}
}
return false;
}
二、记忆化搜索
如果我们使用四维表记录的话,那就太浪费空间了
我们可以减少一维,使用三维表
递归参数设置为
process(char[] str1,int L1,char[] str2,int L2,int size,int[][][] dp)
用size可以推出R1和R2
这样就可以节省空间了
class Solution {
public boolean isScramble(String s1, String s2) {
if ((s1 == null && s2 != null) || (s1 != null && s2 == null)) {
return false;
}
if (s1 == null && s2 == null) {
return true;
}
if (s1.equals(s2)) {
return true;
}
char[] str1=s1.toCharArray();
char[] str2=s2.toCharArray();
int size=str1.length;
int[][][] dp=new int[size][size][size+1];
return process(str1,0,str2,0,size,dp);
}
public boolean process(char[] str1,int L1,char[] str2,int L2,int size,int[][][] dp){
if(dp[L1][L2][size]!=0){
return dp[L1][L2][size]==1;
}
boolean ans = false;
if(size==1){
ans=str1[L1]==str2[L2];
}else{
for(int leftPart=1;leftPart<size;leftPart++){
boolean p1=process(str1,L1,str2,L2,leftPart,dp)
&&process(str1,L1+leftPart,str2,L2+leftPart,size-leftPart,dp);
boolean p2=process(str1,L1,str2,L2+size-leftPart,leftPart,dp)&&
process(str1,L1+leftPart,str2,L2,size-leftPart,dp);
if(p1||p2){
ans=true;
break;
}
}
}
dp[L1][L2][size]=ans?1:-1;
return ans;
}
}
边栏推荐
- Test of countlaunch demo
- 【ACWing】1268. 简单题
- 【Leetcode】493. Reverse Pairs
- Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
- Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
- unity场景跳转脚本
- ALV入门
- Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
- 提升命令行效率的 Bash 快捷键 [完整版]
- 没背景、没学历?专科测试员进入互联网大厂是不是真的没希望?
猜你喜欢

Uniapp applet framework - a set of code, multi segment coverage
![[pytorch] install torch 1.8.1 and check whether torch version and GPU are available](/img/97/078c72729a29675939a84895b5ab86.png)
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available

Mysql优化

Getaverse,走向Web3的远方桥梁

CLM land surface process model

MySQL八股知识点:从入门到删库

地球系统模式(CESM)实践技术

Teach you how to use code to realize SSO single sign on

Common solutions for distributed ID - take one

When AQS wakes up the thread, I understand why it traverses from the back to the front
随机推荐
SAP报表开发步骤
AQS唤醒线程的时候为什么从后向前遍历,我懂了
YOLOv5执行全过程----目录
35. Search the insertion position
Mathematical modeling and optimization analysis based on general optimization software gams
Learn to map with nature medicine -- complex heat map
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available
一次线上事故,我顿悟了异步的精髓
unity场景跳转脚本
Unity scene jump script
Security permission management details
推荐必读:测试人员如何快速熟悉新业务?
关于负数表示的数值为什么比整数大1?
@Autowired注解的原理
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
ALV程序收集
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
真正的科学减肥
C语言-指针进阶
Distance between bus stops: simple simulation problem