问题描述:

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

算法分析:

“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”

此题可以用递归,也可以用动态规划。字符串的题一般用动态规划。

dp[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位

举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第1位,( i + j 位)

从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第1位,( i + j 位)

首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True

举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False

同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)

public class InterleavingString
{
//递归
public boolean isInterleave(String s1, String s2, String s3)
{
if(s1.length() == 0)
{
return s2.equals(s3);//不能用==判断,否则出错
}
if(s2.length() == 0)
{
return s1.equals(s3);
}
if(s3.length() == 0)
{
return s1.length() + s2.length() == 0;
} if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) != s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1));
}
else if(s2.charAt(0) == s3.charAt(0) && s1.charAt(0) != s3.charAt(0))
{
return isInterleave(s1, s2.substring(1), s3.substring(1));
}
else if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) == s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1)) || isInterleave(s1, s2.substring(1), s3.substring(1));
}
else
{
return false;
}
} //动态规划
public boolean isInterleave2(String s1, String s2, String s3)
{
if(s1 == null || s2 == null || s3 == null) return false;
if(s1.length() + s2.length() != s3.length()) return false;
boolean[][] dp = new boolean[s1.length()+1][s2.length()+1];
dp[0][0] = true;
for(int i = 1; i < s1.length() + 1; i ++)
{
if(s1.charAt(i-1) == s3.charAt(i-1) && dp[i-1][0])
{
dp[i][0] = true;
}
}
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(j-1) && dp[0][j-1])
{
dp[0][j] = true;
}
} for(int i = 1; i < s1.length() + 1; i ++)
{
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(i+j-1) && dp[i][j-1])
{
dp[i][j] = true;
}
if(s1.charAt(i-1) == s3.charAt(i+j-1) && dp[i-1][j])
{
dp[i][j] = true;
}
}
}
return dp[s1.length()][s2.length()];
} public static void main(String[] args)
{
String s1 = "aabcc";
String s2 = "dbbca";
String s3 = "aadbbcbcac";
String s4 = "aadbbbaccc";
InterleavingString lv = new InterleavingString();
System.out.println(lv.isInterleave2(s1, s2, s3));
System.out.println(lv.isInterleave2(s1, s2, s4));
}
}

Interleaving String,交叉字符串,动态规划的更多相关文章

  1. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  2. 97. Interleaving String(字符串的交替连接 动态规划)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  3. 097 Interleaving String 交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...

  4. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  5. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  6. 二维动态规划——Interleaving String

    97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...

  7. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  8. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  9. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  10. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

随机推荐

  1. A Magic Lamp---hdu3183(链表删除| RMQ)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 给你一个长度<1000的数a,和m<len(a); 让把数a删除m个数字之后剩下的数 ...

  2. Android Studio 使用小技巧和快捷键

    Android Studio 使用小技巧和快捷键 Alt+回车 导入包,自己主动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 Ctrl+Alt ...

  3. LightOJ1003---Drunk(拓扑排序判环)

    One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...

  4. LocalActivityManager与ActivityGroup

    Helper class for managing multiple running embedded activities in the same process. This class is no ...

  5. pytorch中的cat、stack、tranpose、permute、unsqeeze

    Cat 对数据沿着某一维度进行拼接.cat后数据的总维数不变. 比如下面代码对两个2维tensor(分别为2*3,1*3)进行拼接,拼接完后变为3*3还是2维的tensor. import torch ...

  6. Python np.newaxis

    np.newaxis的功能是插入新维度,看下面的例子: a=np.array([1,2,3,4,5])print a.shape print a 输出结果 (5,)[1 2 3 4 5] 可以看出a是 ...

  7. Spark2.0机器学习系列之8:多类分类问题(方法归总和分类结果评估)

    一对多(One-vs-Rest classifier) 将只能用于二分问题的分类(如Logistic回归.SVM)方法扩展到多类. 参考:http://www.cnblogs.com/CheeseZH ...

  8. Spark Streaming Checkpoint反序列化问题分析

    转载自:https://mp.weixin.qq.com/s/EQgDUSf3TK0oVg1xmg-49Q Checkpoint是Spark Streaming中的核心机制,它为应用程序的7*24小时 ...

  9. oracle的统计信息的查看与收集

    查看某个表的统计信息 SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'; Session altered. SQL&g ...

  10. Objective-C中new与alloc/init的区别

    在实际开发中很少会用到new,一般创建对象我们看到的全是[[className alloc] init],但是并不意味着你不会接触到new,在一些代码中还是会看到[className new],还有去 ...