LeetCode--Interleaving String
来源:互联网 发布:用php编写九九乘法表 编辑:程序博客网 时间:2024/06/17 19:29
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.
思路:动态规划。
这种字符串交错组合的题目是典型的dp问题,状态dp[l1][l2]表示长度l1的s1和长度l2的s2能否通过交错组合找到s3,初始化状态dp[0][0],dp[0][i],dp[i][0]后,状态方程为dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1]).
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int l1=s1.size(),l2=s2.size(),l3=s3.size(); if(l1+l2!=l3) return false; bool dp[l1+1][l2+1]; dp[0][0]=true; for(int i=1;i<=l1;i++){ dp[i][0]=(s1[i-1]==s3[i-1]&&dp[i-1][0]); } for(int i=1;i<=l2;i++){ dp[0][i]=(s2[i-1]==s3[i-1]&&dp[0][i-1]); } for(int i=1;i<=l1;i++){ for(int j=1;j<=l2;j++){ dp[i][j]=(dp[i-1][j]&&s1[i-1]==s3[i+j-1])||(dp[i][j-1]&&s2[j-1]==s3[i+j-1]); } } return dp[l1][l2]; }};
阅读全文
0 0
- LeetCode: Interleaving String
- LeetCode Interleaving String
- LeetCode: Interleaving String
- [Leetcode] Interleaving String
- [LeetCode] Interleaving String
- leetcode 68: Interleaving String
- [Leetcode] Interleaving String
- LeetCode - Interleaving String
- [LeetCode]Interleaving String
- LeetCode: Interleaving String
- Leetcode: Interleaving String
- [Leetcode]Interleaving String
- Leetcode: Interleaving String
- LeetCode:Interleaving String
- LeetCode-Interleaving String
- [leetcode] Interleaving String@DP
- LeetCode:Interleaving String
- LeetCode - Interleaving String
- 排序算法(五)——快速排序
- 成为一名合格软件工程师的7点建议
- Android studio修改包名
- Java中的static关键字
- 查询oracle锁表进程及杀死锁表的进程
- LeetCode--Interleaving String
- linux 常用命令微解
- JMockit 中被 Mocked 的对象属性及方法的默认值
- mysql常用存储引擎简介
- CodeForces
- Android 中内存泄漏的原因分析及解决方案
- 做SEO博客的意义研究
- [喵咪Redis]Redis配置文件和主从设置
- Spring-Data-Jpa projection用法