[LeetCode] Interleaving String 解题思路
Given s1, s2, s3, 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.
问题 : 给定三个字符串 s1, s2, s3, 求 s3 是不是由 s1 和 s2 交错组合而成。
感觉是一条比较典型的 DP 题目。
设 i, j, k 分别是 s1, s2, s3 待求解的当前下标。
- 当 s3[k] != s1[i] 且 s3[k] != s2[j], 则匹配失败
- 当 s3[k] 只和 s1[i] 相等,则 k++ 和 i++
- 当 s3[k] 只和 s2[j] 相等,则 k++ 和 j++
- 当 s3[k] == s1[i] 且 s3[k] == s2[j] ,则分别求 k++ 和 i++ ,以及 k++ 和 j++ ,两者取或运算
由于 s3 恰好有 s1 和 s2 交错行程,所以 s3 长度必然等于 s1 + s2 的长度,知道其中二者就能确定第三个数,这样只需要二维数组保存中间结果即可。
vector<vector<int>> vv; bool interIleave(string s1, string s2, string s3, int p1, int p2, int p3){ // cout << s1 << "\n" << s2 << "\n" << s3 << "\n--------\n"; if (s1.size() == ) {
return (s2 == s3);
} if (s2.size() == ) {
return (s1 == s3);
} if (vv[p1][p3] != -) {
return vv[p1][p3];
} if (s3[] != s1[] && s3[] != s2[]) {
vv[p1][p3] = false;
return false;
} if (s3[] == s1[] && s3[] != s2[]) {
bool tmpb = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} if (s3[] != s1[] && s3[] == s2[]) {
bool tmpb = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
vv[p1][p3] = tmpb;
return tmpb;
} bool inter1 = interIleave(s1.substr(), s2, s3.substr(), p1+, p2, p3+);
bool inter2 = interIleave(s1, s2.substr(), s3.substr(), p1, p2+, p3+);
bool res = inter1 || inter2; vv[p1][p3] = res; return res;
} bool isInterleave(string s1, string s2, string s3) { vector<vector<int>> tmp(s1.size(), vector<int>(s3.size(), -));
vv = tmp; if (s3.size() != s1.size()+s2.size()) {
return false;
} if (s3.size() == ) {
return true;
} bool res = interIleave(s1, s2, s3, , , ); return res;
}
[LeetCode] Interleaving String 解题思路的更多相关文章
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Interleaving String [30]
题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...
- [leetcode]Interleaving String @ Python
原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [Leetcode] Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
随机推荐
- ajax 的基本原理
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- char *s = getpass()屏幕不回显示 ,返回输入的字符
char *s = getpass(“please input you name:”)屏幕不回显示 ,返回输入的字符
- 使用WebBrowser的记录
第一:新建一个类,用了获取WebBrowser元素的类 //需要引用 Interop.SHDocVw 和 Microsoft.mshtmlpublic class Element { //根据Name ...
- mysql 清空表 Truncate及delete区别
1.delete from 表名[where]; 2.truncate table 表名; 3.delete将mysql表中所有记录一条一条删除到删完 4.truncate保留mysql表的结构,重新 ...
- VHDL程序的库
VHDL库存储和放置了可被其他VHDL程序调用的数据定义.器件说明.程序包等资源.VHDL库的种类有很多,但最常见的库有IEEE标准库.WORK库.IEEE标准库主要包括STD_LOGIC_1164. ...
- 关于Java(介绍)
基于Java官方指导文档,开展学习 Java是什么 Java 是编程语言,也是一个平台 特性 简单 可移植 面向对象 分布式运算高性能 健壮 安全 动态 体系结构中立 记忆宫殿:恋爱是件简单的事,但放 ...
- 数据采集服务提供商,ip提供商 里面有些不错的基础数据
http://user.qzone.qq.com/1649677458 这家公司的爬虫应该挺牛的 !@#!#!~#¥¥¥@@http://www.site-digger.com/
- uva 1396 - Most Distant Point from the Sea
半平面的交,二分的方法: #include<cstdio> #include<algorithm> #include<cmath> #define eps 1e-6 ...
- IAR中 C语言位定义
__IO_REG8_BIT( SYS, 0xFFFFF802, __READ_WRITE ) #define __IO_REG8_BIT(NAME, ADDRESS, A ...
- Spring factorybean
自定义FactoryBean 需要实现FactoryBean接口 通过FacotryBean来配置bean的实例. class: 指向FactoryBean的全类名 property:配置Factor ...