Python program to check matching of simple parentheses

后端 未结 25 2405
谎友^
谎友^ 2020-12-01 17:02

I am a Python newbie and I came across this exercise of checking whether or not the simple brackets \"(\", \")\" in a given string are matched evenly.

I have seen ex

25条回答
  •  我在风中等你
    2020-12-01 17:36

    The problem with your approach is that you don't consider the order. Following line would pass: ))) (((. I'd suggest to keep the count of open and closed parenthesis:

    • counter starts from 0
    • every ( symbol increment counter
    • every ) symbol decrements counter
    • if at any moment counter is negative it is an error
    • if at the end of the line counter is 0 - string has matching parenthesis

提交回复
热议问题