Parenthesis/Brackets Matching using Stack algorithm

前端 未结 30 2731
你的背包
你的背包 2020-11-27 11:39

For example if the parenthesis/brackets is matching in the following:

({})
(()){}()
()

and so on but if the parenthesis/brackets is not mat

30条回答
  •  隐瞒了意图╮
    2020-11-27 12:05

    The algorithm:

    1. scan the string,pushing to a stack for every '(' found in the string
    2. if char ')' scanned, pop one '(' from the stack

    Now, parentheses are balanced for two conditions:

    • '(' can be popped from the stack for every ')' found in the string, and
    • stack is empty at the end (when the entire string is processed)

提交回复
热议问题