
97. Interleaving String
发布日期:2021-05-09 02:50:43
浏览次数:17
分类:博客文章
本文共 1444 字,大约阅读时间需要 4 分钟。
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"Output: false
Approach #1: DP. [C++]
class Solution {public: bool isInterleave(string s1, string s2, string s3) { //if (s1 == "" && s2 == "" && s3 == "") return true; //if (s1 == "" || s2 == "" || s3 == "") return false; if (s1.length() + s2.length() != s3.length()) return false; vector> match(s1.length()+1, vector (s2.length()+1, false)); match[0][0] = true; for (int idx = 0; idx < s1.length() + s2.length(); ++idx) { for (int s1Len = 0; s1Len <= idx+1 && s1Len <= s1.length(); ++s1Len) { int s2Len = idx + 1 - s1Len; if (s2Len > s2.length()) continue; if ((s1Len > 0 && match[s1Len-1][s2Len] && s3[idx] == s1[s1Len-1]) || (s2Len > 0 && match[s1Len][s2Len-1] && s3[idx] == s2[s2Len-1])) match[s1Len][s2Len] = true; } } return match[s1.length()][s2.length()]; }};
������
Analysis:
status: match[s1Len][s2Len] represent s1Len characters in s1 and s2Len characters in s2 wether match with s1Len+s2Len characters in s3.
init: match[0][0] = true;
func:
result: match[s1.length()][s2.length()].
发表评论
最新留言
不错!
[***.144.177.141]2025年04月23日 01时03分28秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
成功解决升级virtualenv报错问题
2021-05-11
【SQLI-Lab】靶场搭建
2021-05-11
Xception 设计进化
2021-05-11
【Bootstrap5】精细学习记录
2021-05-11
Hololens2开发笔记-捕获照片到内存并上传至服务器(unity)
2021-05-11
SkyWalking性能剖析
2021-05-11
LeetCode197.打家劫舍
2021-05-11
A simple problem HDU-2522 【数学技巧】
2021-05-11
487-3279 POJ-1022【前导0~思维漏洞】
2021-05-11
Struts2-从值栈获取list集合数据(三种方式)
2021-05-11
vue-axios的总结及项目中的常见封装方法。
2021-05-11
Linux之磁盘管理
2021-05-11
vscode中快速生成vue模板
2021-05-11
HTML5 Web Storage
2021-05-11
ERP项目成功的关键因素:团队建设
2021-05-11
demo---购物车的多条记录保存(cookie)
2021-05-12
数据链路访问
2021-05-12
参考图像
2021-05-12