Task is to check if given string contains balanced sets of {}
, []
and ()
.
For example, check(\"{[}]\")
must return
Please tell me what is wrong with my code it's always giving NO
string isBalanced(string s) {
stack comp;
char temp;
int i;
for(i=s.size()-1;(s[i]=='}' ) || (s[i]==')' ) || (s[i]==']');i--)
{
comp.push(s[i]);
}
if(comp.size()!=i+1)
return ("NO");
while(!comp.empty() && i>=0)
{
temp=comp.top();
if(temp!=s[i])
return ("NO");
comp.pop();
i--;
}
return("YES");
}