-
Notifications
You must be signed in to change notification settings - Fork 160
/
BinaryMatching.cpp
73 lines (51 loc) · 1.27 KB
/
BinaryMatching.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
/*
Given a string including 1, 0 and ?, return all matching strings.
Char ? can match 0 and 1.
For example:
Input : 1??
Output: {100, 101, 110, 111}.
*/
/*
solution1: backtracking.
O(k^2*(n-k)) time, O(1) space, k is the number of ? in string with size n.
*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void GenerateMatchStringInner(string s, int index, vector<string> &result) {
if (index == s.size()) {
result.push_back(s);
return;
}
if (s[index] == '?') {
string temp1 = s;
temp1[index] = '0';
GenerateMatchStringInner(temp1, index+1, result);
string temp2 = s;
temp2[index] = '1';
GenerateMatchStringInner(temp2, index+1, result);
} else {
GenerateMatchStringInner(s, index+1, result);
}
}
vector<string> GenerateMatchString(const string &s) {
int n = s.size();
vector<string> result;
if ( n == 0) {
return result;
}
GenerateMatchStringInner(s, 0, result);
return result;
}
int main() {
string str ="1??";
vector<string> result = GenerateMatchString(str);
for (size_t i = 0; i < result.size(); ++i) {
cout<<result[i]<<endl;
}
return 0;
}
/*
Solution2: iterative method using stack
*/