问题描述:

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. 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20

    原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可

  2. python yield的终极解释

    (译)Python关键字yield的解释(stackoverflow): http://stackoverflow.com/questions/231767/the-python-yield-keyw ...

  3. win下如何解决在chrome的同源访问问题

    引子:本来是想验证如果在网页中包含多个框架,那么就会存在两个以上的不同全局环境,如果从一个框架引用另一个框架的数据比如数组a,那么用 instanceof 判断这个数组a是不是另个框架Array的实例 ...

  4. MySQL中的索引详讲

    一.什么是索引?为什么要建立索引? 索引用于快速找出在某个列中有一特定值的行,不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的 ...

  5. 细说PHP7

    PHP7带来的新东西 1.类型的声明. 可以使用字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool),来声明函数的参数类型与函数返回值. declare(s ...

  6. 20165324《Java程序设计》第四周

    学号 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第五章:子类与继承 子类的定义:class 子类名 extends 父类名 { ... } 子类继承性: ...

  7. 以二进制方式读取图片保存到string

    procedure TForm1.BitBtn1Click(Sender: TObject);var  StringStream : TStringStream;  FSize : integer;  ...

  8. WEB前端研发工程师编程能力成长之路(2)

    四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综合考虑,给出最优方案.以一招胜万招. var s ...

  9. zip解压破解

    B2TQZJEpzFAUHcHaTQRjtKi8C4Q5mpFsBFLYsNTfCs7ZD65X

  10. BFC 详说 Block Formatting Contexts (块级格式化上下文)

    定位方案是控制元素的布局,在 CSS 2.1 中,有三种定位方案——普通流 (Normal Flow) .浮动 (Floats) 和绝对定位 (Absolute Positioning) ,下面分别对 ...