-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbracesS.cpp
More file actions
60 lines (57 loc) · 1.27 KB
/
Copy pathbracesS.cpp
File metadata and controls
60 lines (57 loc) · 1.27 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//((a+b))
//(a-b*(c+d))
#include<bits/stdc++.h>
using namespace std;
bool redundant(string str){
stack<char> s;
if(str.length() == 0){
return false;
}
/*for(int i=0; i<str.length(); i++){ // another method but not considering the other example
if(str.at(i) == ')'){
char currTop = s.top();
s.pop();
int n = 0;
while(currTop != '('){
n++;
currTop = s.top();
s.pop();
}
if(n<=1){
return 1;
}
}else{
s.push(str.at(i));
}
}
return false;*/
for(int i=0; i<str.length(); i++){
if(str.at(i) == ')'){
char t = s.top();
s.pop();
int flag = 1;
while(t != '('){
if(t =='+' || t =='-' || t =='*' || t =='/')
flag = 0;
t = s.top();
s.pop();
}
if(flag == 1){
return 1;
}
}else{
s.push(str.at(i));
}
}
return 0;
}
int main(){
string str;
cin >> str;
if(redundant(str)){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
}