-
Notifications
You must be signed in to change notification settings - Fork 0
/
interleaving_string.cpp
123 lines (109 loc) · 3.25 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// https://oj.leetcode.com/problems/interleaving-string/
// Date: Nov 27, 2014
#include <iostream>
#include <string>
#include <set>
#include <cassert>
using namespace std;
class Solution {
public:
bool isInterleave(string s1, string s2, string s3)
{
if ((s1.size()+s2.size()) != s3.size())
{
return false;
}
if (s3.size() == 0)
{
return true;
}
set<pair<int,int> > pair_set;
// populate combination for position #1
if (s2.size() > 0)
{
if (s2[0] == s3[0])
{
pair_set.insert(make_pair(-1,0));
}
}
if (s1.size() > 0)
{
if (s1[0] == s3[0])
{
pair_set.insert(make_pair(0,-1));
}
}
if (pair_set.size() == 0)
{
return false;
}
bool flag = true;
for (int k=1; k != s3.size(); ++k)
{
set<pair<int,int> > success_pair_set;
set<pair<int,int> > failed_pair_set;
for (set<pair<int,int> >::iterator it = pair_set.begin(); it != pair_set.end(); ++it)
{
int p = (*it).first;
int q = (*it).second;
assert(((p+q+2) == k) && "p,q incorrect");
// s1[p],s2[q+1]
if ((q+1) < s2.size())
{
if ( (success_pair_set.find(make_pair(p,q+1)) == success_pair_set.end()) &&
(failed_pair_set.find(make_pair(p,q+1)) == failed_pair_set.end()) )
{
if (s2[q+1] == s3[p+q+2])
{
success_pair_set.insert(make_pair(p,q+1));
}
else
{
failed_pair_set.insert(make_pair(p,q+1));
}
}
}
// a1[p+1],s2[q]
if ((p+1) < s1.size())
{
if ( (success_pair_set.find(make_pair(p+1,q)) == success_pair_set.end()) &&
(failed_pair_set.find(make_pair(p+1,q)) == failed_pair_set.end()) )
{
if (s1[p+1] == s3[p+q+2])
{
success_pair_set.insert(make_pair(p+1,q));
}
else
{
failed_pair_set.insert(make_pair(p+1,q));
}
}
}
}
if (success_pair_set.empty())
{
flag = false;
break;
}
else
{
pair_set = success_pair_set;
}
}
return flag;
}
};
int main(int argc, char* argv[])
{
string s1 = "aabcc";
string s2 = "dbbca";
string s3 = "aadbbcbcac";
Solution sln;
bool flag = sln.isInterleave(s1,s2,s3);
cout << flag << endl;
return 0;
}
/*
C++11 does not provide a hash for pairs (or tuples), even of hashable types.
http://stackoverflow.com/questions/21288345/unordered-set-of-pairs-compilation-error
*/