Skip to content

Commit

Permalink
Merge pull request #18 from NAMAN-Github01/main
Browse files Browse the repository at this point in the history
Added balanced bracket problem
  • Loading branch information
Gyanthakur authored Oct 6, 2024
2 parents 94a3532 + 2bb6596 commit 3cb1534
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions october_2024/balanced_brackets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;

unordered_map<char, int> bracket = {{'(', 1}, {'{', 2}, {'[', 3}, {')', -1}, {'}', -2}, {']', -3}};
string isBalanced(string s) {
stack<char> st;
for(char i:s){
if(bracket[i] > 0) {
st.push(i);
} else {
if(st.empty()) return "NO";
char top = st.top();
st.pop();
if(bracket[top] + bracket[i] != 0) {
return "NO";
}
}
}
if(st.empty()) return "YES";
return "NO";
}

int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
cout << isBalanced(s) << endl;
}
}

0 comments on commit 3cb1534

Please sign in to comment.