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; } };
欢迎关注微信公众号——计算机视觉
相关推荐
今天我们要探讨的是LeetCode平台上的一道中等难度的问题——“Interleaving String”,即交错字符串问题,具体到“97-interleaving-string.js”这个文件,它包含了使用JavaScript语言对该问题的解答。 ...
在探讨LeetCode中第97题“Interleaving String”的Python解法之前,我们需要先理解题目的基本要求和背景。这道题目属于动态规划(Dynamic Programming)的经典问题,通常要求我们判断是否存在一种方式可以将两个或多...
0097题,即“Interleaving String”,要求解者判断是否存在一种方法,能够将两个字符串s3,s1和s2交织成s3。具体来说,这个问题是这样的:给定三个字符串s1、s2和s3,判断s3是否是由s1和s2交织而成。交织是指s3中的...
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 ...