Add stacks for c-or-cpp (#80)

pull/83/head
Ritwik Vaidya 2021-02-22 07:03:58 +05:30 committed by GitHub
parent 84d85814e2
commit 8cb86de0d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

5
stacks/README.md 100644
View File

@ -0,0 +1,5 @@
# Stacks
### C or C++
1. [Balanced Paranthesis](c-or-cpp/balanced-paranthesis.cpp)

View File

@ -0,0 +1,43 @@
#include<bits/stdc++.h>
using namespace std;
bool isBalanced(string s) {
stack<char> st;
char ch;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
continue;
}
if (st.empty())
return false;
switch (s[i]) {
case ')':
ch = st.top();
st.pop();
if (ch == '{' || ch == '[')
return false;
break;
case '}':
ch = st.top();
st.pop();
if (ch == '(' || ch == '[')
return false;
break;
case ']':
ch = st.top();
st.pop();
if (ch == '(' || ch == '{')
return false;
break;
}
}
return (st.empty());
}
int main() {
string input = "{}[({})]";
if (isBalanced(input))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}