Leetcode#97 Interleaving String
转化为二维地图游走问题。
比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^"表示开始位置,"$"表示终止位置。
a a b <- s2
s1 -> a ^ . .
b . . .
a . . .
b . . $
现在问题变成了,能否走出一条等于s3的路径?
对于地图上的任意一点,不妨设坐标为(i, j),令k为当前坐标下s3对应的位置。
如果s1[i]=s3[k]则可以向下走
如果s2[j]=s3[k]则可以向右走
否则意味着无路可走了
令res[i][j]表示s1[i..s1.length-1]和s2[j..s2.length-1]能否组成s3[k..s3.length],其中s3.length-k=(s1.length-i) + (s2.length-j)
则有递推公式:res[i][j] = (s1[i]等于s3[k],且 res[i+1][j]) 或 (s2[j]等于s3[k],且res[i][j+1])
由于计算res[i][j]时只用到了res[i+1][j]或res[i][j+1](相邻层),所以在具体编码实现的时候可以进行状态压缩,用一维数组保存res[i][j]
代码:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
vector<bool> res(len2 + , false);
if (len1 + len2 != len3) return false;
if (!len1) return s2 == s3;
if (!len2) return s1 == s3;
res[len2] = true;
for (int j = len2 - ; j >= ; j--)
res[j] = s3[len3 - (len2 - j)] == s2[j] && res[j + ];
for (int i = len1 - ; i >= ; i--) {
for (int j = len2 - ; j >= ; j--) {
int k = len3 - (len1 - i) - (len2 - j);
res[j] = (s3[k] == s1[i] && res[j]) || (s3[k] == s2[j] && res[j + ]);
}
}
return res[];
}
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能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 【一天一道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 ...
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- leetcode@ [97] Interleaving Strings
https://leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by th ...
- [Leetcode][JAVA] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- canvas 绘制矩形和圆形
canvas绘制有两神方法:1).填充(fill)填充是将图形内部填满. 2).绘制边框 (stroke)绘制边框是不把图形内部填满,只是绘制图形的外框. 当我们在绘制图形的时候,首先要设定好绘制的样 ...
- 批量删除的js代码
<script type="text/javascript"> function seltAll(){ var chckBoxSign = document.getEl ...
- 双栈排序(codevs 1170)题解
[问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈 ...
- 一个自定义的C#数据库操作基础类 SqlHelper
SqlHelper其实是我们自己编写的一个类,使用这个类目的就是让使用者更方便.更安全的对数据库的操作,既是除了在SqlHelper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的 ...
- 关键字替换排除HTML标签属性字符
解决办法: 1.打开文件e/class/functions.php 2.找到函数 ReplaceKey($newstext,$classid=0) 3.找到替换代码 if(STR_IREPLACE) ...
- Moses更改权重的命令变化 -d -t -
-l 可以用: weight-l 或者lm (不需要在前面加-) 还是用-weight-overwrite “Distortion0= 0"更保险 reording weight i ...
- Drawable
今天简单的介绍一下有关以下5中的应用: Statelistdrawable Layerdrawable Shapeddrawable Clipdrawable Animationdrawable 1. ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- hdu 1250 Hat's Fibonacci
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...
- Windows Phone中In-App Purchase应用内购买
前言 应用内购买(In-App Purchase)对于开发者来说绝对是一个非常重要的功能,它提供了一个便捷的入口供用户来购买付费.在IAP盛行之前的游戏运营商一般都是通过接入第三方支付入口 ...