-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
longest_substring_with_matching_parentheses.cc
66 lines (58 loc) · 1.82 KB
/
longest_substring_with_matching_parentheses.cc
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
#include <algorithm>
#include <stack>
#include <string>
#include <vector>
#include "test_framework/generic_test.h"
using std::max;
using std::stack;
using std::string;
using std::vector;
int LongestMatchingParentheses(const string& s) {
int max_length = 0, end = -1;
stack<int> left_parentheses_indices;
for (int i = 0; i < size(s); ++i) {
if (s[i] == '(') {
left_parentheses_indices.emplace(i);
} else if (empty(left_parentheses_indices)) {
end = i;
} else {
left_parentheses_indices.pop();
int start = empty(left_parentheses_indices)
? end
: left_parentheses_indices.top();
max_length = max(max_length, i - start);
}
}
return max_length;
}
template <typename IterType>
int ParseFromSide(char paren, IterType begin, IterType end) {
int max_length = 0, num_parens_so_far = 0, length = 0;
for (IterType i = begin; i < end; ++i) {
if (*i == paren) {
++num_parens_so_far, ++length;
} else { // *i != paren
if (num_parens_so_far <= 0) {
num_parens_so_far = length = 0;
} else {
--num_parens_so_far, ++length;
if (num_parens_so_far == 0) {
max_length = max(max_length, length);
}
}
}
}
return max_length;
}
int LongestMatchingParenthesesConstantSpace(const string& s) {
return max(ParseFromSide('(', begin(s), end(s)),
ParseFromSide(')', rbegin(s), rend(s)));
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"s"};
return GenericTestMain(args, "longest_substring_with_matching_parentheses.cc", "longest_substring_with_matching_parentheses.tsv", &LongestMatchingParentheses,
DefaultComparator{}, param_names);
}
// clang-format on