-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBad_Sequence.cpp
More file actions
49 lines (48 loc) · 816 Bytes
/
Bad_Sequence.cpp
File metadata and controls
49 lines (48 loc) · 816 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
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin>>n;
string s; cin>>s;
if(n%2)
{
cout<<"No";
return 0;
}
int op = 0, cl = 0;
for(int i=0; i<n; i++)
{
if(s[i]=='(')
op++;
else
cl++;
}
if(op!=cl)
{
cout<<"No";
return 0;
}
stack<char> st;
int cnt = 0;
for(int i=0; i<n; i++)
{
if(s[i]=='(')
st.push(s[i]);
else
{
if(st.empty())
{
cnt++;
}
else if(st.top()=='(')
{
st.pop();
}
}
}
if(cnt<=1)
cout<<"Yes";
else
cout<<"No";
return 0;
}