-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidbraces.cpp
More file actions
115 lines (99 loc) · 3.42 KB
/
validbraces.cpp
File metadata and controls
115 lines (99 loc) · 3.42 KB
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
/* After trying for 2 hours and failing ... I finally gave up and searched Google and found the following code that works. My attempt is at the bottom. Mine works for everything except ({})[({})] and )(}{][
Challenge is found here: https://www.codewars.com/kata/5277c8a221e209d3f6000b56
Source: https://www.tutorialspoint.com/valid-parentheses-in-cplusplus
I added all the comments to help me learn what is going on. Yes, I am BRAND NEW to C++
*/
#include <iostream>
#include <stack>
using namespace std;
bool isBalancedExp(string exp) {
stack<char> stk; // create a stack, which is LIFO - last in, first out
char x;
for (int i=0; i<exp.length(); i++) { // will iterate through whole string
if (exp[i]=='('||exp[i]=='['||exp[i]=='{') { // checks to see if character is an opening brace
stk.push(exp[i]); // if opening brace exists, put it on top of stack, closing braces never get put on stack
continue;
}
if (stk.empty()) // if the stack is empty, end
return false;
switch (exp[i]) { // switch checks closing braces to see if it matches opening brace on top of stack
case ')':
x = stk.top(); // assign top opening brace in stack to x
stk.pop(); // then remove top element from stack
if (x=='{' || x=='[') // if closing brace in exp[i] does not match opening brace in x, return false
return false;
break;
case '}':
x = stk.top();
stk.pop();
if (x=='(' || x=='[')
return false;
break;
case ']':
x = stk.top();
stk.pop();
if (x =='(' || x == '{')
return false;
break;
} // end switch
} // If no closing brace is found, returns to for loop and gets next opening brace to put on top of stack
return (stk.empty()); // when for loop ends, empty the stack
}
int main() {
string expresion = "()[(){()}]";
if (isBalancedExp(expresion))
cout << "This is Balanced Expression";
else
cout << "This is Not Balanced Expression";
}
/*
#include <iostream>
#include <string>
using namespace std;
bool valid_braces(std::string braces) {
int length = braces.size();
bool pairs = true;
// cout << "length of braces is " << braces.size() << endl;
// cout << "Bool pairs begins at " << pairs << endl;
// first check for odd number of braces for easy false
if (length % 2 == 1) {
pairs = false;
}
else {
// Convert brace pairs to letters for comparison purposes
for (int i = 0; i < length; i++) {
if (braces[i] == '(' || braces[i] == ')') {
braces[i] = 'a';
}
else if (braces[i] == '[' || braces[i] == ']') {
braces[i] = 'b';
}
else if (braces[i] == '{' || braces[i] == '}') {
braces[i] = 'c';
}
}
cout << "New string: " << braces << endl;
for (int i = 0; i < length-1; i++) {
// cout << "Int i is: " << i << endl;
if (braces[i] == braces[i+1] ) {
// cout << "checking together: " << braces[i] << " and " << braces[i+1] << endl;
i++;
}
else if (braces[i] == braces[length-1]) {
// cout << "checking ends: " << braces[i] << " and " << braces[length - 1] << endl;
length--;
}
// else if (braces[i] == ) { }
else {
pairs = false;
cout << pairs << endl;
}
}
}
cout << pairs << endl;
return pairs;
}
int main() {
valid_braces("({})[({})]");
}
*/