97. 交错字符串
https://leetcode-cn.com/problems/interleaving-string/
难度中等580
给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。
两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
s = s1 + s2 + ... + snt = t1 + t2 + ... + tm|n - m| <= 1- 交错 是
s1 + t1 + s2 + t2 + s3 + t3 + ...或者t1 + s1 + t2 + s2 + t3 + s3 + ...
提示:a + b 意味着字符串 a 和 b 连接。
示例 1:

输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" 输出:true
示例 2:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" 输出:false
示例 3:
输入:s1 = "", s2 = "", s3 = "" 输出:true
提示:
0 <= s1.length, s2.length <= 1000 <= s3.length <= 200s1、s2、和s3都由小写英文字母组成
通过次数64,708提交次数142,242
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length() != s3.length()) return false;
boolean [][] temp = new boolean[s1.length()+1][s2.length()+1];
int index = 0;
temp[0][0] = true;
for(int i=0;i<=s1.length();i++)
{
for(int j=0;j<=s2.length();j++)
{
index = i+j-1;
if(i>0) temp[i][j] = temp[i-1][j] && s1.charAt(i-1)==s3.charAt(index)||temp[i][j];
if(j>0) temp[i][j] = (temp[i][j-1]&&s2.charAt(j-1)==s3.charAt(index))||temp[i][j];
}
}
return temp[s1.length()][s2.length()];
}
}

该博客主要探讨了如何解决LeetCode中的中等难度问题——交错字符串。通过给出的示例和代码解析,解释了如何判断一个字符串是否由另外两个字符串交错组成,并提供了具体的Java实现。算法基于动态规划思想,构建一个二维布尔数组来存储状态,从而确定s3是否符合条件。
578

被折叠的 条评论
为什么被折叠?



