File tree 1 file changed +67
-0
lines changed
1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ void balance_parentheses ()
4
+ {
5
+ stack<char > st;
6
+ string str;
7
+ cout << " Enter string may or may not containing parentheses:: " ;
8
+ cin >> str;
9
+ int flag = 0 ; // flag is an arbitrary variable
10
+ for (int i = 0 ; i < str.size ();i++)
11
+ // for length of the string calculated by number of letters
12
+ {
13
+ if (str[i] == ' {' || str[i] == ' [' || str[i] == ' (' )
14
+ {
15
+ st.push (str[i]); // push function to enter terms in a stack;
16
+ flag = 1 ;
17
+ }
18
+ if (!st.empty ())
19
+ {
20
+ if (str[i] == ' }' )
21
+ {
22
+ if (st.top () == ' {' ) // top of the stack;
23
+ {
24
+ st.pop (); // pop function to delete terms from the top of array;
25
+ continue ;
26
+ }
27
+ else
28
+ break ;
29
+ }
30
+ if (str[i] == ' ]' )
31
+ {
32
+ if (st.top () == ' [' )
33
+ {
34
+ st.pop ();
35
+ continue ;
36
+ }
37
+ else
38
+ break ;
39
+ }
40
+ if (str[i] == ' )' )
41
+ {
42
+ if (st.top () == ' (' )
43
+ {
44
+ st.pop ();
45
+ continue ;
46
+ }
47
+ else
48
+ break ;
49
+ }
50
+ }
51
+ else
52
+ break ;
53
+ }
54
+ if ((st.empty ()) && (flag == 1 ))
55
+ cout << " YES" << endl;
56
+ else
57
+ cout << " NO" << endl;
58
+ }
59
+ int main ()
60
+ {
61
+ int t;
62
+ cout << " Enter number of test cases:: " ;
63
+ cin >> t;
64
+ for (int i = 0 ; i < t; i++)
65
+ balance_parentheses (); // calling of function for checking of brackets;
66
+ return 0 ;
67
+ }
You can’t perform that action at this time.
0 commit comments