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.
方法:感觉方法很笨。可能是dp状态找的不好。
本方法:
S(i,j) = 1
if (s1[i+1] == s3[i+j+1]) S(i+1,j) = 1
if (s2[j+1] == s3[i+j+1]) S(i,j+1) = 1
如有好的方法,请告诉我啊!!
#include <queue> #include <utility> using namespace std; class Solution { public: bool m[100][100]; bool isInterleave(string s1, string s2, string s3) { memset(m, 0, sizeof(m)); if (s1.size() + s2.size() != s3.size()) return false; queue<pair<int,int> > q; q.push(make_pair(0,0)); int i1, i2, t; while (!q.empty()) { i1 = q.front().first; i2 = q.front().second; q.pop(); t = i1 + i2; if (t == s3.size()) return true; if (i1 < s1.size() && s3[t] == s1[i1]) { if (!m[i1+1][i2]){ m[i1+1][i2] = true; q.push(make_pair(i1+1, i2)); } } if (i2 < s2.size() && s3[t] == s2[i2]) { if (!m[i1][i2+1]) { m[i1][i2+1] = true; q.push(make_pair(i1, i2+1)); } } } return false; } };
做完后,发现之前做过。
dp[i][j] = true, 表示s1[0~i]和s2[0~j]可以搞成s3[i+j]
dp[i][j] =
1. true: dp[i-1][j] = true && s1[i] == s3[i+j]
2. true: dp[i][j-1] = true && s2[j] == s3[i+j]
3. false: else
class Solution { public: bool isInterleave(string s1, string s2, string s3) { int l1 = s1.size(), l2 = s2.size(), l3 = s3.size(); if (l3 != l1 + l2) return false; bool dp[l1+1][l2+1]; memset(dp, 0, sizeof(dp)); dp[0][0] = true; for (int len = 1; len <= l3; len++) { for (int i = 0; i <= len && i <= l1; i++) { int j = len - i; if (j > l2) continue; dp[i][j] = false; if (i > 0 && dp[i-1][j] && s1[i-1] == s3[len-1]) dp[i][j] = true; if (j > 0 && dp[i][j-1] && s2[j-1] == s3[len-1]) dp[i][j] = true; } } return dp[l1][l2]; } };
相关推荐
leetcode 答案 LeetCode-String #这是LeetCode里面String专题中的python版答案
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ...
leetcode字符串转换为整数
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
Reverse words in a string-leetcode
"interleaving_string.erl","search_insert_position.erl", "three_sum.erl","trapping_rain_water.erl", "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑
大佬的leetcode刷题笔记(c++版本)
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
leetcode中文版
100个leetCode详细解答
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
LeetCode 刷题汇总1
LeetCode 选讲1
leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 双指针 编号 题目 LeetCode 11 Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 ...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。