leetcode #97 in cpp
来源:互联网 发布:淘宝卖虚拟商品 编辑:程序博客网 时间:2024/05/20 23:08
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.
Solution:
A simple idea is to keep 3 pointers p1, p2, p3, each on s1, s2, and s3. We could only move pointers p1 or p2 forward if current character p1 or p2 points to matches the character p3 points to. When p1 and p2 point characters both of which do not match p3's character, we move back p1 and p2 back to last match location and try a different move.
Say s1 = "ab", s2 = "ac", and s3 = "abac".
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
both p1 and p2 matches p3, we move p3. We can move p1 or p2. we choose to move p2.
[a(p1),b]
[a,c(p2)]
[a,b(p3),a,c]
neither p1 nor p2 matches p3. We step back to last match and try a different move.
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
[a,b(p1)]
[a(p2), c]
[a,b(p3),a,c].
p1 matches p3. move p1 and p3.
[a,b](p1)
[a(p2), c]
[a,b,a(p3),c].
[a,b](p1)
[a, c(p2)]
[a,b,a,c(p3)].
p2 matches p3. We are done.
bool check(string &s1, string &s2, string &s3, int ind1, int ind2, int ind3){ if(ind1 == s1.length() && ind2 == s2.length() && ind3 == s3.length()){ return true; } if(ind3 >= s3.length() || (ind1 >= s1.length() && ind2 >= s2.length()) ) return false; bool res = false; if(ind1<s1.length() && ind2 < s2.length() && s1[ind1]!=s3[ind3] && s2[ind2] != s3[ind3]) return false; if(ind1 < s1.length()){ if(s1[ind1] == s3[ind3]){ if(check(s1,s2,s3, ind1+1, ind2, ind3+1)) return true; } else if(ind2 >= s2.length()) return false; } if(ind2 < s2.length()){ if(s2[ind2] == s3[ind3]){ if(check(s1,s2,s3,ind1, ind2+1, ind3+1)) return true; }else if(ind1>=s1.length()) return false; } return false; }
Code:
bool isInterleave(string s1, string s2, string s3) { int len1 = s1.length(); int len2 = s2.length(); int len3 = s3.length(); if(len1+len2!=len3) return false; if(s1[0] != s3[0] && s2[0] != s3[0]) return false; vector<vector<bool>> dp(len1+1, vector<bool>(len2+1,false)); dp[0][0] = true; for(int i = 1; i <= len1; i ++){ dp[i][0] = s1.substr(0, i) == s3.substr(0,i)?true:false; } for(int i = 1; i <= len2; i ++){ dp[0][i] = s2.substr(0, i) == s3.substr(0,i)?true:false; } //dp[i][j] = true if s1[0...i-1] s2[0...j-1] matches s3[i+j-1] for(int i = 1; i <= len1; i++){ for(int j = 1; j <= len2; j ++){ if(dp[i-1][j]){ dp[i][j] = dp[i][j] || s1[i-1] == s3[i+j-1]; } if(dp[i][j-1]){ dp[i][j] =dp[i][j] || s2[j-1] == s3[i+j-1]; } } } return dp[len1][len2]; }
- leetcode #97 in cpp
- leetcode #12 in cpp
- Leetcode #13 in cpp
- Leetcode #14 in cpp
- leetcode %15 in cpp
- leetcode #16 in cpp
- leetcode #17 in cpp
- leetcode #18 in cpp
- leetcode #20 in cpp
- leetcode #21 in cpp
- leetcode #22 in cpp
- leetcode #23 in cpp
- leetcode #24 in cpp
- leetcode #25 in cpp
- leetcode #26 in cpp
- leetcode #27 in cpp
- leetcode #28 in cpp
- leetcode #29 in cpp
- Python Strip()函数
- iOS 将Log日志重定向输出到文件中保存
- 关于C的复习
- Mac OS X用当前选中的文字生成便签的快捷键是什么
- java字符串
- leetcode #97 in cpp
- SharePoint Document Library中的"Versioning Settings"功能与CSOM的对应
- JSF基本搭建流程
- Time类中的运算符重载
- How to use Qt’s QSortFilterProxyModel
- python中关于with及contextlib的用法
- C# 泛型的协变和逆变
- 第15周 项目1---程序填空
- python-24点智取游戏