forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckBrackets.cpp
More file actions
59 lines (58 loc) · 1.16 KB
/
Copy pathCheckBrackets.cpp
File metadata and controls
59 lines (58 loc) · 1.16 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
#include<bits/stdc++.h>
using namespace std;
int same(char a, char b)
{
if (a == '[' && b == ']')
return 1;
if (a == '{' && b == '}')
return 1;
if (a == '(' && b == ')')
return 1;
return 0;
}
int check(char *a)
{
char stack[1001], top = -1;
for (int j = 0; j < strlen(a); j++)
{
if (a[j] == '[' || a[j] == '{' || a[j] == '(')
stack[++top] = a[j];
if (a[j] == ']' || a[j] == '}' || a[j] == ')')
{
if (top == -1)
{
return 0;
}
else
{
if (!same(stack[top--], a[j]))
{
return 0;
}
}
}
}
if (top != -1)
{
return 0;
}
return 1;
}
int main()
{
char a[1001];
int n, valid;
cout<<"Enter number of bracket sequences you need to input: ";
cin>>n;
for (int i = 0; i < n; i++)
{
cout<<"Enter the bracket sequence "<<(i+1)<<" :\n";
cin>>a;
valid = check(a);
if (valid == 1)
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
}