#include <bits/stdc++.h>
using namespace std;
bool Balanced(string size_t)
{
stack<char> p;
char n;
for (int i = 0; i < size_t.length(); i++)
{
if (size_t[i] == '(' || size_t[i] == '['
|| size_t[i] == '{')
{
p.push(size_t[i]);
continue;
}
if (p.empty())
return false;
switch (size_t[i]) {
case ')':
n = p.top();
p.pop();
if (n == '{' || n == '[')
return false;
break;
case '}':
n = p.top();
p.pop();
if (n == '(' || n == '[')
return false;
break;
case ']':
n = p.top();
p.pop();
if (n == '(' || n == '{')
return false;
break;
}
}
return (p.empty());
}
int main()
{
string size_t = "( { ) }";
if (Balanced(size_t))
cout << "Balanced";
else
cout << "UnBalanced";
return 0;
}
Comments
Leave a comment