Bracket Matching
/*
Bracket Matching
Example 1:
Input:
{([])}
Output:
true
Explanation:
{ ( [ ] ) }. Same brackets can form
balanced pairs, with 0 number of
unbalanced bracket.
Example 2:
Input:
()
Output:
true
Explanation:
(). Same bracket can form balanced pairs,
and here only 1 type of bracket is
present and in balanced way.
Example 3:
Input:
([]
Output:
false
Explanation:
([]. Here square bracket is balanced but
the small bracket is not balanced and
Hence , the output will be unbalanced.
Your Task:
This is a function problem. You only need to complete the function ispar() that takes a string as a parameter and returns a boolean value true if brackets are balanced else returns false. The printing is done automatically by the driver code.
Expected Time Complexity: O(|x|)
Expected Auxiliary Space: O(|x|)
Constraints:
1 ≤ |x| ≤ 32000
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool ispar(string x)
{
int open_parentheses = 0, open_curly = 0, open_square = 0;
int close_parentheses = 0, close_curly = 0, close_square = 0;
int y;
for (int i = 0; i < x.size(); i++)
{
if (x[i] == '(')
open_parentheses += 1;
if (x[i] == '{')
open_curly += 1;
if (x[i] == '[')
open_square += 1;
if (x[i] == ']')
close_square += 1;
if (x[i] == '}')
close_curly += 1;
if (x[i] == ')')
close_parenthesis += 1;
if ((open_parenthesis < close_parentheses) || (open_square < close_square) || (open_curly < close_curly))
return false;
}
if (((open_parentheses - close_parentheses) == 0) && ((open_square - close_square) == 0) && ((open_curly - close_curly) == 0))
return true;
return false;
}
};
int main()
{
int t;
string a;
cin >> t;
while (t--)
{
cin >> a;
Solution obj;
if (obj.ispar(a))
cout << "balanced" << endl;
else
cout << "not balanced" << endl;
}
}
Comments
Post a Comment