bool balanced(string e)
{
stack<char> st;
char n;
for (int i = 0; i < e.length(); i++)
{
if (e[i] == '(' || e[i] == '['
|| e[i] == '{')
{
st.push(e[i]);
continue;
}
if (st.empty())
return false;
switch (e[i]) {
case ')':
n = st.top();
st.pop();
if (n == '{' || n == '[')
return false;
break;
case '}':
n = st.top();
st.pop();
if (n == '(' || n == '[')
return false;
break;
case ']':
n = st.top();
st.pop();
if (n == '(' || n == '{')
return false;
break;
}
}
return (st.empty());
}
Comments
Leave a comment