-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparens.cpp
160 lines (148 loc) · 4.03 KB
/
parens.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <string_view>
#include <chrono>
#include <functional>
static bool isValidParens(const std::string_view str)
{
std::size_t checker = 0;
for(const auto& ch : str)
{
if(ch == '(')
checker += 1;
else if(ch == ')')
{
if(checker > 0)
checker -= 1;
else return false;
}
}
return true;
}
static void generatePermutations3(std::unordered_map<char, std::size_t>& freqTable,
std::size_t n,
std::string& buffer,
std::vector<std::string>& permutations)
{
if(buffer.size() == n)
{
if(isValidParens(buffer))
permutations.push_back(buffer);
return;
}
for(auto& pair : freqTable)
{
if(pair.second > 0)
{
pair.second -= 1;
buffer.push_back(pair.first);
generatePermutations3(freqTable, n, buffer, permutations);
buffer.pop_back();
pair.second += 1;
}
}
}
static std::unordered_map<char, std::size_t> buildFreqTable(const std::string_view str)
{
std::unordered_map<char, std::size_t> map;
for(const auto& ch : str)
map[ch] += 1;
return map;
}
static std::vector<std::string> generatePermutations3(const std::string_view str)
{
std::unordered_map<char, std::size_t> freqTable = buildFreqTable(str);
std::string buffer;
std::vector<std::string> permutations;
generatePermutations3(freqTable, str.size(), buffer, permutations);
return permutations;
}
static std::vector<std::string> validParens1(const std::size_t n)
{
std::string str;
for(std::size_t i = 0; i < n; ++i)
str.append("()");
return generatePermutations3(str);
}
static void validParens2(std::size_t openCount, std::size_t closeCount, std::string& str, std::vector<std::string>& parens)
{
// If all places hold either '(' or ')' then we have reached to one possible valid permutation
if((openCount + closeCount) == str.size())
{
parens.push_back(str);
return;
}
// The number of open parentheses can never greater than the half of the number of chars in the string,
// Because, there needs to be as many close parentheses to cancel the open parentheses, totalling the number of chars to the size of the string.
// So, here we only add an open parentheses if the current number of open parantheses is less than the half of the string's size.
if(openCount < (str.size() / 2))
{
str[openCount + closeCount] = '(';
// Recursively generate next valid '(' or ')'
validParens2(openCount + 1, closeCount, str, parens);
}
// Only add close parantheses if the current number of close parantheses is less than the open parantheses
if(openCount > closeCount)
{
str[openCount + closeCount] = ')';
// Recursively generate next valid '(' or ')'
validParens2(openCount, closeCount + 1, str, parens);
}
}
static std::vector<std::string> validParens2(const std::size_t n)
{
std::string str(n * 2, ' ');
std::vector<std::string> parens;
validParens2(0, 0, str, parens);
return parens;
}
struct Solution1
{
std::vector<std::string> operator()(const std::size_t n)
{
return validParens1(n);
}
};
struct Solution2
{
std::vector<std::string> operator()(const std::size_t n)
{
return validParens2(n);
}
};
template<typename Sol>
static void runValidParens(const std::size_t n)
{
std::cout << "Input: " << n << "\n";
auto start = std::chrono::steady_clock::now();
auto validParens = Sol { } (n);
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(end - start).count();
std::cout << "Number of valid parens: " << validParens.size() << "\n";
std::cout << "Time taken: " << elapsed << " ms\n";
#ifdef DUMP
for(std::size_t i = 0; const auto& parens : validParens)
std::cout << "[" << i++ << "] " << parens << "\n";
#endif
}
static void run(const std::size_t n)
{
static std::size_t runCount = 0;
++runCount;
std::cout << "-----------RUN: " << runCount << " ------------\n";
std::cout << "**Solution no 1**\n";
runValidParens<Solution1>(n);
std::cout << "**Solution no 2**\n";
runValidParens<Solution2>(n);
}
int main()
{
run(3);
run(4);
run(5);
run(6);
run(10);
return 0;
}