-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interleaving String.cpp
84 lines (64 loc) · 2.35 KB
/
Interleaving String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int p1 = 0,len1 = s1.size();
int p2 = 0,len2 = s2.size();
int p3;
map<vector<int>,int> Mymap;
vector<int> MymapElement = {0,0,0};
vector<vector<int>> TraceRoute;
for( p3 = 0; p3<s3.size(); p3++) {
MymapElement[0] = p1;
MymapElement[1] = p2;
MymapElement[2] = p3;
// the element has been visited
if (Mymap[MymapElement] == -1) {
if(TraceRoute.size() == 0)
return false;
vector<int> Element;
Element = TraceRoute[TraceRoute.size() - 1];
TraceRoute.erase(TraceRoute.end() - 1);
p1 = Element[0];
p2 = Element[1];
p3 = Element[2];
continue;
}
Mymap[MymapElement] = -1;
if (p1 < len1 && p2 < len2 && s3[p3] == s1[p1] && s3[p3] == s2[p2]){
vector<int> Element;
Element.push_back(p1);
Element.push_back(p2 + 1);
Element.push_back(p3);
TraceRoute.push_back(Element);
p1++;
continue;
}
if (p1 < len1 && s3[p3] == s1[p1]) {
p1++;
continue;
}
if (p2 < len2 && s3[p3] == s2[p2]) {
p2++;
continue;
}
/*
if(p1<len1 && p2<len2 && s3[p3]!=s1[p1] && s3[p3] !=s2[p2]){
*/
if (TraceRoute.size() == 0)
return false;
vector<int> Element;
Element = TraceRoute[TraceRoute.size() - 1];
TraceRoute.erase(TraceRoute.end() - 1);
p1 = Element[0];
p2 = Element[1];
p3 = Element[2];
continue;
/*}
*/
}
//the string s1 and s2 happens to end
if (p1 == s1.size() && p2 == s2.size() && p3 == s3.size())
return true;
return false;
}
};