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 = ...
随机推荐
- 史上最全Vim快捷键键位图(入门到进阶)
经典版 下面这个键位图应该是大家最常看见的经典版了. 对应的简体中文版 其实经典版是一系列的入门教程键位图的组合结果,下面是不同编辑模式下的键位图. 入门版 基本操作的入门版. 进阶版 增强版 下图是 ...
- ubuntu上搭建vsftpd且通过mysql来管理FTP账号
参考文章:http://wiki.ubuntu.org.cn/Vsftpd%E5%92%8Cmysql%E9%85%8D%E7%BD%AE 请各位先按照这篇文章一步一步操作,我这里是记录一些其间遇到的 ...
- ASP.NET MVC5学习笔记之Filter基本介绍
Filter是ASP.NET MVC框架提供的基于AOP(面向方面)设计,提供在Action执行前后做一些非业务逻辑通用处理,如用户验证,缓存等.现在来看看Filter相关的一些类型信息. 一.基本类 ...
- 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...
- DM9000网卡的基本工作原理
MAC:主要负责数据帧的创建,数据差错,检查,传送控制等. PHY:物理接口收发器,当收到MAC过来的数据时,它会加上校验码,然后按照物理层的规则进行数据编码,再发送到传输介质上,接收过程则相反. M ...
- 006-python基础-条件判断与循环
一.条件判断 场景一.用户登陆验证 # 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! #!/usr/bin/env pytho ...
- Python学习教程(learning Python)--1.3 Python数据输入
多数应用程序都有数据输入语句,用于读入数据,和用户进行交互,在Python语言里,可以通过raw_input函数实现数据的从键盘读入数据操作. 基本语法结构:raw_input(prompt) 通常p ...
- [转]ORACLE的ProC用法讲解
pro*c是高级的用法,OCI是oracle的基础用法 如何编译.pc文件: proc code=cpp parse=none iname=filename.pc oname=filename.cp ...
- EMVTag系列5《8E 持卡人验证方法(CVM)列表》
L: var. up to 252 -R(需求):数据必须存在,在读应用数据过程中,终端不检查 按照优先顺序列出卡片应用支持的所有持卡人验证方法 注:一个应用中可以有多个CVM列表,例如一个用于国内交 ...
- SAP B1 ADDON 开发
承接各类SAP B1 ADDON 开发. 有需要,请联系.