-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathparanthesis.cpp
More file actions
39 lines (34 loc) · 792 Bytes
/
paranthesis.cpp
File metadata and controls
39 lines (34 loc) · 792 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
Problem Description
Given a string A consisting only of '(' and ')'.
You need to find whether parantheses in A is balanced or not ,if it is balanced then return 1 else return 0.
Solution:
int Solution::solve(string str) {
stack<char>stk;
stk.push('x');
int i=0;
if(str[0]==')'){
return 0;
}
if(str[str.size()-1]=='('){
return 0;
}
while(i<str.size()){
if(str[i]=='('){
stk.push(str[i]);
}
if(str[i]==')'){
while(stk.top()!='('){
if(stk.top()=='x'){
return 0;
}
stk.pop();
}
stk.pop();
}
i++;
}
if(stk.top()=='x'){
return 1;
}
return 0;
}