| 14 | } |
| 15 | |
| 16 | int main() |
| 17 | { |
| 18 | int t; // t is no of test cases |
| 19 | cin>>t; |
| 20 | while(t--) |
| 21 | { |
| 22 | string s; |
| 23 | cin>>s; |
| 24 | stack<char> st; // declare stack st |
| 25 | for(int i=0;i<s.length();i++) // iterate through the string |
| 26 | { |
| 27 | if(!st.empty() && areMatchingBrackets(st.top(),s[i])) //If the current character is a closing bracket (‘)‘ or ‘}‘ or ‘]‘) then check if it is matching bracket. |
| 28 | st.pop(); // if matching bracket then pop from the stack |
| 29 | else |
| 30 | st.push(s[i]); // If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack. |
| 31 | } |
| 32 | if(st.empty()) |
| 33 | cout<<"balanced"<<endl; |
| 34 | else |
| 35 | cout<<"not balanced"<<endl; |
| 36 | } |
| 37 | |
| 38 | return 0; |
| 39 | } |
nothing calls this directly
no test coverage detected