LeetCode 97: Interleaving String staggered, flip String leetcode
Blog reprint please note address: http://blog.csdn.net/SunliyMonkey/article/details/48165711
Description
Address: https://leetcode.com/problems/interleaving-string/
Three strings S1 , S2 , S3 , Judge S3 Whether S1 , S2 In combination.
Example:
S1 = aabcc
S2 = dbbca
When S3 = aadbbcbcac Returns true.
When S3 = aadbbbaccc Returns false.
Site trap
None
Code
class Solution {public: bool isInterleave(string s1, string s2, string s3) { vector <int> match[2]; int cur, nxt; int l; int l1 = s1.length(); int l2 = s2.length(); int l3 = s3.length(); if(l1+l2 != l3) return false; int *vis[2]; vis[0] = new int[l1+1]; vis[1] = new int[l1+1]; for(int i = 0; i <= l1; i++) vis[0][i] = vis[1][i] = -1; nxt = 0; match[0].push_back(0); for(int i = 0; i < l3; i++){ cur = nxt; nxt = !nxt; match[nxt].clear(); for(int k = 0; k < match[cur].size(); k++){ l = match[cur][k]; if(l < l1 && s1[l] == s3[i]){ if(vis[nxt][l+1] != i){ match[nxt].push_back(l+1); vis[nxt][l+1] = i; } } if(i-l+1 <= l2 && s2[i-l] == s3[i]){ if(vis[nxt][l] != i){ match[nxt].push_back(l); vis[nxt][l] = i; } } } } return !match[nxt].empty(); }};
Ranking
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Blog address: http://blog.csdn.net/sunliymonkey