-
Notifications
You must be signed in to change notification settings - Fork 0
/
076_Valid_Parentheses.cpp
164 lines (124 loc) · 3.51 KB
/
076_Valid_Parentheses.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
161
162
163
164
/*
Codewars Coding Challenge
Day 76/366
Level 7kyu
Valid Parentheses
Write a function that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it's invalid.
Examples
"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true
Constraints
0 <= length of input <= 100
All inputs will be strings, consisting only of characters ( and ).
Empty strings are considered balanced (and therefore valid), and will be tested.
For languages with mutable strings, the inputs should not be mutated.
Protip: If you are trying to figure out why a string of parentheses is invalid, paste the parentheses into the code editor, and let the code highlighting show you!
#include <string>
bool validParentheses(const std::string& str) {
return false;
}
https://www.codewars.com/kata/6411b91a5e71b915d237332d/train/cpp
*/
// My Solutions
#include <string>
#include <stack>
#include <iostream>
bool validParentheses(const std::string& str) {
std::stack<char> parenStack;
for(char c : str){
if(c == '('){
parenStack.push(c);
}else if (c == ')'){
if (parenStack.empty()){
return false; // kurung tutup tanpa pasangan
}else{
parenStack.pop(); // hapus kurung buka teratas
}
}
}
return parenStack.empty(); // return true jika semua kurung buka memiliki pasangan
}
// Test cases
int main(){
std::cout << validParentheses("(") << std::endl;
std::cout << validParentheses("(())((()())())") << std::endl;
std::cout << validParentheses("()") << std::endl;
std::cout << validParentheses(")(()))") << std::endl;
}
/*
Sample Tests
#include <string>
bool validParentheses(const std::string& str);
// TODO: Replace examples and use TDD by writing your own tests
Describe(SampleTests)
{
It(ValidCases)
{
dotest("()", true);
dotest("((()))", true);
dotest("()()()", true);
dotest("(()())()", true);
dotest("()(())((()))(())()", true);
}
It(EmptyString) {
dotest("", true);
}
It(InvalidCases) {
dotest(")(", false);
dotest("()()(", false);
dotest("((())", false);
dotest("())(()", false);
dotest(")()", false);
dotest(")", false);
}
private:
void dotest(const std::string& str, bool expected) {
bool actual = validParentheses(str);
auto msg = ExtraMessage("Incorrect answer for input = \"" + str + "\"");
Assert::That(actual, Equals(expected), msg);
}
};
*/
/*
Solutions from codewars
=1=
#include <string>
bool validParentheses(const std::string& str) {
int open = 0;
for(char c : str) {
if((open += c == '(' ? 1 : -1) < 0) {
return false;
}
}
return !open;
}
=2=
#include <regex>
#include <string>
bool validParentheses(const std::string& str) {
try {
return std::regex{str}, true;
} catch (std::regex_error) {
return false;
}
}
=3=
bool validParentheses( const std::string& str ) {
std::stack< char > s;
for ( const char c : str )
if ( c == '(' ) s.push( c );
else if ( !s.empty() && s.top() == '(' ) s.pop();
else return false;
return s.empty();
}
=4=
bool validParentheses(const std::string& str){
int a = 0;
for(char c : str)
if((a += c == '(' ? 1 : -1) < 0)
return 0;
return !a;
}
*/