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.
typedef struct ConflictPoint { int i; int j; int k; int flag; // 1 means go from s1 before, while 2 means s2 ConflictPoint(int i, int j, int k):i(i), j(j), k(k) {} } CP; class Solution { public: bool isInterleave(string s1, string s2, string s3) { int l1 = s1.size(); int l2 = s2.size(); int l3 = s3.size(); if(l1 + l2 != l3) return false; int i = 0, j = 0, k = 0; stack<CP> sp; for(; k < l3;) { bool conflict = false; if(s3[k] == s1[i] && s3[k] == s2[j]) { CP cp(i, j, k); sp.push(cp); conflict = true; } if(s3[k] == s1[i]) { ++k; ++i; if(conflict) sp.top().flag = 1; } else if(s3[k] == s2[j]) { ++k; ++j; if(conflict) sp.top().flag = 2; } else { if(!sp.empty()) { CP cp = sp.top(); sp.pop(); i = cp.i; j = cp.j; k = cp.k; if(cp.flag == 1) { ++k; ++j; } else if(cp.flag == 2) { ++k; ++i; } } else { return false; } } } return true; } };
欢迎关注微信公众号——计算机视觉
相关推荐
python python_leetcode题解之097_Interleaving_String
js js_leetcode题解之97-interleaving-string.js
c语言基础 c语言_leetcode题解之0097_interleaving_string.zip
leetcode 分类 ...Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
97.Interleaving String, 115.Distinct Subsequences 2017/04/24 (Lintcode)92.Backpack, (Lintcode)125.Backpack II, (Lintcode)564.Backpack VI 不适用 53.Maximum Subarray, 91.Decode Ways, 96.Unique Binary ...
leetcode卡 OJ 题目汇总 记录下自己做过的oj题目, 也记录下自己对算法以及编程的理解与偏见. 杂感 算法到底是有用还是没用? 这个问题一直萦绕在我的心里. ...String动态规划解决, Recovering BST失败
"interleaving_string.erl","search_insert_position.erl", "three_sum.erl","trapping_rain_water.erl", "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑
在编程领域,交错或交织(Interleaving)是一种字符串组合方式,它涉及到将两个或多个字符串的字符交替地组合在一起,形成新的字符串。这个过程在算法设计中有时会被用来处理字符串相关的问题,例如构建字符串的解析...
Processing (interleaving) record buffer is done in the background to not block the main thread and the UI. Also WAV conversion for export is also quite heavy for longer recordings, so best left to ...