How to check if a String is balanced?

前端 未结 8 1054
逝去的感伤
逝去的感伤 2020-12-06 11:57

I want to test if an input String is balanced. It would be balanced if there is a matching opening and closing parenthesis, bracket or brace.

example:
{} bal         


        
8条回答
  •  春和景丽
    2020-12-06 12:46

    Well, roughly speaking, if it is balanced, that means your stack should be empty.

    For that, you need to pop your stack when you parse a }

    Additional requirement is to check that } is preceded by { or the character popped is a {.

提交回复
热议问题