[leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
题意:
给定s1和s2,判断给定的s3是不是s1和s2交织相错后可以生成的字符串

思路:
遇到字符串的子序列或匹配问题巴普洛夫狗流哈喇子实验般的想到dp
s1 = 0 "a a b c c",
0 T
s2 = "d
b
b
c
a",
------------------------ s3 = "aadbbcbcac"
初始化:
dp[0][0] = true
考虑是否需要预处理第一个row: dp[0][j] ? 需要!处理极端情况即s3的字符完全来自s1,则if s1.charAt(j-1) == s3.charAt(j-1) , dp[0][j] = dp[0][j-1]
考虑是否需要预处理第一个col : dp[i][0] ? 需要!处理极端情况即s3的字符完全来自s2,则if s2.charAt(i-1) == s3.charAt(i-1) , dp[i][0] = dp[i-1][0]
对于dp[i][j]
s3下一个字符,要么来自s1,要么来自s2
dp[i][j] = (dp [i-1][j] && s2.charAt(i-1) == s3.charAt(i + j -1) )
||(dp [i][j-1] && s1.charAt(j-1) == s3.charAt(i + j -1) );
【注意,之前错写成】
if s2.charAt(i-1) == s3.charAt(i + j -1), dp [i][j] = dp [i-1][j]
if s1.charAt(j-1) == s3.charAt(i + j -1), dp [i][j] = dp [i][j-1]
为何错? 因为dp[i][j] 若此时等于'b' 而此时s1有'b' , s2有'b', dp[i][j] 就会两个if语句都进入,最终被先后赋值两次。
代码:
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if(s1.length() + s2.length() != s3.length()) return false;
boolean[][] dp = new boolean[s2.length() + 1][s1.length() + 1];
// init
dp[0][0] = true;
for(int i = 1; i<= s2.length(); i++){
if(s2.charAt(i-1) == s3.charAt(i-1)) {
dp[i][0] = dp[i-1][0];
}
}
for(int j = 1; j <= s1.length(); j++){
if(s1.charAt(j-1) == s3.charAt(j-1)) {
dp[0][j] = dp[0][j-1];
}
}
for(int i = 1; i<= s2.length(); i++){
for(int j = 1; j <= s1.length(); j++){
dp[i][j] = (dp [i-1][j] && s2.charAt(i-1) == s3.charAt(i + j -1) )
||(dp [i][j-1] && s1.charAt(j-1) == s3.charAt(i + j -1) );
}
}
return dp[s2.length()][s1.length()];
}
}
[leetcode]97. Interleaving String能否构成交错字符串的更多相关文章
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- leetcode 97 Interleaving String ----- java
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- Leetcode#97 Interleaving String
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 97. Interleaving String(字符串的交替连接 动态规划)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- python 的 format 函数
python的格式化字符串方法之一------------format 函数 它通过{}和:来代替%. 数字 格式 输出 描述 3.1415926 {:.2f} 3.14 保留小数点后两位 3.141 ...
- zombodb 索引管理
zombodb 支持标准的index 管理(create .alter.drop) 创建索引 CREATE INDEX index_name ON table_name USING zombodb ( ...
- 关于Java流
- [蓝桥杯]ALGO-187.算法训练_P0502
编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20.然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来.如果有两个元素值出 ...
- AsyncTask(异步任务)
一.AsyncTask ①AsyncTask的源码: public abstract class AsyncTask<Params, Progress, Result> 三种泛型类型分别代 ...
- Reference.svcmap: Could not load file or assembly
Reference.svcmap: Could not load file or assembly 在添加服务的时候出现如上错误, 把这个勾去掉就可以了.
- 模拟实现简单ATM功能
- js解决转义字符问题
数据“\\s=7\\c=1\\j=1\\p=1”, 转义出来变成“\s=7\c=1\j=1\p=1” 结果:可以这样转换str=str.replace(/\\/g,'\\\\');
- ES6 入门Promise
Promise是一个对象用来传递异步操作的消息,有三种状态:Pending(进行中),Resolved(已完成又称Fulfilled)和Rejected(已失败). 特点:对象状态不受外界的影响.一旦 ...
- win7、centos7 双系统安装总结
centos7安装过程 问题:TroubleShooting选项进入图形化界面安装才成功. win7恢复引导区 问题:安装完Centos后,win7的引导区不见了 具体恢复过程:http://www. ...