题目:
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
这道题的图表的原始图:
接下来是i=0和j=0的情况:
s3=aadbbcbcac
dp[i][j]就是S1[0....I-1],S2[0....J-1],S3[I+j-1]是不是也满足交错字符。
代码如下:
package test;
public class LC97Try4
{
public boolean isInterleave(String s1, String s2, String s3)
{
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != l1 + l2)
{
return false;
}
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
char[] c3 = s3.toCharArray();
boolean[][] dp = new boolean[l1 + 1][l2 + 1];
dp[0][0] = true;
for (int j = 1; j <= l2; j++)
{
if (c2[j - 1] == c3[j - 1] && dp[0][j - 1])
{
dp[0][j] = true;
}
else
{
break;
}
}
for (int i = 1; i <= l1; i++)
{
if (c1[i - 1] == c3[i - 1] && dp[i - 1][0])
{
dp[i][0] = true;
}
else
{
break;
}
}
for (int i = 1; i <= l1; i++)
{
for (int j = 1; j <= l2; j++)
{
if (dp[i - 1][j] && c1[i - 1] == c3[i + j - 1])
{
dp[i][j] = true;
continue;
}
if (dp[i][j - 1] && c2[j - 1] == c3[i + j - 1])
{
dp[i][j] = true;
}
}
}
return dp[l1][l2];
}
public static void main(String[] args)
{
LC97Try4 t = new LC97Try4();
System.out.println(t.isInterleave("aabcc", "dbbca", "aadbbbaccc"));
}
}
空间压缩的代码如下:
package test;
public class LC97Try5
{
public boolean isInterleave(String s1, String s2, String s3)
{
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != l1 + l2)
{
return false;
}
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
char[] c3 = s3.toCharArray();
char[] shorts = c1;
int sl = l1;
int longl = l2;
char[] longs = c2;
if (l2 < l1)
{
shorts = c2;
longs = c1;
sl = l2;
longl = l1;
}
boolean[] dp = new boolean[sl + 1];
dp[0] = true;
for (int i = 0; i <= longl; i++)
{
for (int j = 0; j <= sl; j++)
{
if(i==0&&j==0){
continue;
}
if (i > 0 && (dp[j] && longs[i - 1] == c3[i + j - 1]))
{
dp[j] = true;
continue;
}
if (j > 0 && (dp[j - 1] && shorts[j - 1] == c3[i + j - 1]))
{
dp[j] = true;
continue;
}
dp[j]=false;
}
}
return dp[sl];
}
public static void main(String[] args)
{
LC97Try5 t = new LC97Try5();
System.out.println(t.isInterleave("a", "", "a"));
}
}
我的超时的代码如下:
package test;
public class LC97Try1
{
public boolean isInterleave(String s1, String s2, String s3)
{
boolean ret = false;
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != l1 + l2)
{
return false;
}
if(l3==0){
return true;
}
int m = 0;
int n = 0;
int i = 0;
for (i = 0; i < l3; i++)
{
if (m == l1 || n == l2)
{
break;
}
if (s1.charAt(m) == s3.charAt(i) && s2.charAt(n) == s3.charAt(i))
{
if(m==l1-1){
return isInterleave(s1.substring(m), s2.substring(n + 1),s3.substring(i+1));
}
if(n==l2-1){
return isInterleave(s1.substring(m + 1), s2.substring(n),s3.substring(i+1));
}
if (isInterleave(s1.substring(m + 1), s2.substring(n),
s3.substring(i+1))
|| isInterleave(s1.substring(m), s2.substring(n + 1),
s3.substring(i+1)))
{
return true;
}
else
{
return false;
}
}
if (s1.charAt(m) == s3.charAt(i))
{
m++;
}
else
{
if (s2.charAt(n) == s3.charAt(i))
{
n++;
}
else
{
return false;
}
}
}
if (n <=l2 - 1)
{
if (s2.substring(n).equals(s3.substring(i)))
{
return true;
}
else
{
return false;
}
}
if (m <= l1 - 1)
{
if (s1.substring(m).equals(s3.substring(i)))
{
return true;
}
else
{
return false;
}
}
return ret;
}
public static void main(String[] args)
{
LC97Try1 t = new LC97Try1();
System.out.println(t.isInterleave("aabcc", "dbbca", "aadbbbaccc"));
}
}
还有一个超时的:哈哈哈
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LC97Try3
{
public boolean isInterleave(String s1, String s2, String s3)
{
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
if (l3 != l1 + l2)
{
return false;
}
if (l3 == 0)
{
return true;
}
boolean ret = false;
List<Integer> list = new ArrayList<Integer>();
Map<Integer, Integer> value = new HashMap<Integer, Integer>();
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
char[] c3 = s3.toCharArray();
int[] tag = new int[l3];
int m = 0;
int n = 0;
int i = 0;
tag[l3-1]=-1;
for (i = 0; i < l3; i++)
{
if ((m < l1 && c1[m] == c3[i]) && (n < l2 && c2[n] == c3[i]))
{
list.add(i);
value.put(i, n);
tag[i] = m;
m++;
}
else
{
if (m < l1 && c1[m] == c3[i])
{
tag[i] = m;
m++;
}
else
{
if (n < l2 && c2[n] == c3[i])
{
tag[i] = n;
n++;
}
else
{
if (list.isEmpty())
{
return false;
}
else
{
i = list.get(list.size() - 1);
tag[i] = value.get(i);
m = i - tag[i];
n = tag[i]+1;
list.remove(list.size() - 1);
}
}
}
}
}
if (tag[l3 - 1] != -1)
{
return true;
}
return ret;
}
public static void main(String[] args)
{
LC97Try3 t = new LC97Try3();
System.out.println(t.isInterleave("a", "", "a"));
}
}
总之这两种都是超时的。哈哈哈
哈哈哈
2021年7月1日
超时的代码---回溯超时
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()){
return false;
}
if(s1.length()==0){
return s2.equals(s3);
}
if(s2.length()==0){
return s1.equals(s3);
}
return getPass3(0,0,0,s1,s2,s3);
}
public boolean getPass3(int i,int j,int k,String s1,String s2,String s3){
if(k==s3.length()){
return true;
}
if(i<s1.length()&&s1.charAt(i)==s3.charAt(k)){
boolean ret=getPass3(i+1,j,k+1,s1,s2,s3);
if(ret){
return true;
}
}
if(j<s2.length()&&s2.charAt(j)==s3.charAt(k)){
boolean ret=getPass3(i,j+1,k+1,s1,s2,s3);
if(ret){
return true;
}
}
return false;
}
}
2、动态规划1
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()){
return false;
}
if(s1.length()==0){
return s2.equals(s3);
}
if(s2.length()==0){
return s1.equals(s3);
}
int l1=s1.length();
int l2=s2.length();
boolean[][] p=new boolean[l1+1][l2+1];//从s1的i 到 s2 的j 是不是满足 s3的i+j呢
p[0][0]=true;
for(int i=1;i<=l2;i++){
if(s2.charAt(i-1)==s3.charAt(i-1)&&p[0][i-1]){
p[0][i]=true;
}else{
break;
}
}
for(int i=1;i<=l1;i++){
if(s1.charAt(i-1)==s3.charAt(i-1)&&p[i-1][0]){
p[i][0]=true;
}else{
break;
}
}
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(s1.charAt(i-1)==s3.charAt(i+j-1) && p[i-1][j]){
p[i][j]=true;
}else{
if(s2.charAt(j-1)==s3.charAt(i+j-1)&&p[i][j-1]){
p[i][j]=true;
}
}
}
}
return p[l1][l2];
}
}
动态规划优化1
public boolean isInterleave2(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()){
return false;
}
int l1=s1.length();
int l2=s2.length();
boolean[][] p=new boolean[l1+1][l2+1];//从s1的i 到 s2 的j 是不是满足 s3的i+j呢
p[0][0]=true;
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if((i>0&&p[i-1][j])&& s1.charAt(i-1)==s3.charAt(i+j-1)){
p[i][j]=true;
}
if((j>0&&p[i][j-1])&&s2.charAt(j-1)==s3.charAt(i+j-1)){
p[i][j]=true;
}
}
}
return p[l1][l2];
}
动态规划---空间优化
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length()+s2.length()!=s3.length()){
return false;
}
int l1=s1.length();
int l2=s2.length();
boolean[] p=new boolean[l2+1];//从s1的i 到 s2 的j 是不是满足 s3的i+j呢
p[0]=true;
for(int i=0;i<=l1;i++){
for(int j=0;j<=l2;j++){
if(i>0){
p[j]= p[j] && s1.charAt(i-1)==s3.charAt(i+j-1);
}
if(j>0){
p[j]=p[j]||p[j-1]&&s2.charAt(j-1)==s3.charAt(i+j-1);
}
}
}
return p[l2];
}
哈哈哈