forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_paranthesis.cpp
More file actions
39 lines (36 loc) · 826 Bytes
/
balanced_paranthesis.cpp
File metadata and controls
39 lines (36 loc) · 826 Bytes
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
// Check For Balanced Paranthesis in a string
#include<bits/stdc++.h>
#include<stack>
using namespace std;
bool matching(char a, char b)
{
return((a=='(' && b==')')) || ((a=='{') && b=='}') || ((a=='[' && b==']'));
}
bool isBalanced(string str)
{
stack<int>s;
for(int i=0;i<str.length();i++)
{
if(str[i]=='(' || str[i]=='{' || str[i]=='[')
{
s.push(str[i]);
}
else
{
if(s.empty()==true)
return false;
else if(matching(s.top(),str[i]==false))
return false;
else
s.pop();
}
}
return(s.empty()==true); // to handle extra opening brackets
}
int main()
{
string str;
cin>>str;
isBalanced(str);
return 0;
}