Skip to content

Commit a1e0ae1

Browse files
authored
Merge pull request #440 from ishan-pf/ishan-pf
Added the stl approach for solving the balanced parenthesis problem
2 parents b015974 + 1f74bbf commit a1e0ae1

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].
2+
3+
Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and ().
4+
5+
A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {[(])} is not balanced because the contents in between { and } are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket, (, and the pair of parentheses encloses a single, unbalanced closing square bracket, ].
6+
7+
By this logic, we say a sequence of brackets is balanced if the following conditions are met:
8+
9+
It contains no unmatched brackets.
10+
The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.
11+
12+
Given
13+
14+
strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, return YES. Otherwise, return NO.
15+
16+
Function Description
17+
18+
Complete the function isBalanced in the editor below.
19+
20+
isBalanced has the following parameter(s):
21+
22+
string s: a string of brackets
23+
24+
Returns
25+
26+
string: either YES or NO
27+
28+
Input Format
29+
30+
The first line contains a single integer
31+
, the number of strings.
32+
Each of the next lines contains a single string
33+
34+
, a sequence of brackets.
35+
36+
Constraints
37+
38+
, where
39+
40+
is the length of the sequence.
41+
All chracters in the sequences ∈ { {, }, (, ), [, ] }.
42+
43+
Output Format
44+
45+
For each string, return YES or NO.
46+
47+
Sample Input
48+
49+
STDIN Function ----- -------- 3 n = 3 {[()]} first s = '{[()]}' {[(])} second s = '{[(])}' {{[[(())]]}} third s ='{{[[(())]]}}'
50+
51+
Sample Output
52+
53+
YES
54+
NO
55+
YES
56+
57+
Explanation
58+
59+
The string {[()]} meets both criteria for being a balanced string.
60+
The string {[(])} is not balanced because the brackets enclosed by the matched pair { and } are not balanced: [(]).
61+
The string {{[[(())]]}} meets both criteria for being a balanced string.
62+
*/
63+
64+
65+
// solution:
66+
67+
#include <bits/stdc++.h>
68+
69+
using namespace std;
70+
71+
string ltrim(const string &);
72+
string rtrim(const string &);
73+
74+
string isBalanced(string s) {
75+
76+
unordered_map<char,int>symbols{{'{',-1},{'[',-2},{'(',-3},{'}',1},{']',2},{')',3}};
77+
78+
stack<char>st;
79+
80+
for( char bracket: s){
81+
82+
if(symbols[bracket] < 0) st.push(bracket);
83+
84+
else {
85+
if(st.empty())return "NO";
86+
char top = st.top();
87+
st.pop();
88+
if( symbols[bracket] + symbols[top] != 0) return "NO";
89+
}
90+
}
91+
if(!st.empty())return "NO";
92+
return "YES";
93+
}
94+
95+
int main()
96+
{
97+
ofstream fout(getenv("OUTPUT_PATH"));
98+
99+
string t_temp;
100+
getline(cin, t_temp);
101+
102+
int t = stoi(ltrim(rtrim(t_temp)));
103+
104+
for (int t_itr = 0; t_itr < t; t_itr++) {
105+
string s;
106+
getline(cin, s);
107+
108+
string result = isBalanced(s);
109+
110+
fout << result << "\n";
111+
}
112+
113+
fout.close();
114+
115+
return 0;
116+
}
117+
118+
string ltrim(const string &str) {
119+
string s(str);
120+
121+
s.erase(
122+
s.begin(),
123+
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
124+
);
125+
126+
return s;
127+
}
128+
129+
string rtrim(const string &str) {
130+
string s(str);
131+
132+
s.erase(
133+
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
134+
s.end()
135+
);
136+
137+
return s;
138+
}

0 commit comments

Comments
 (0)