Task with braces, brackets and parenthesis

前端 未结 5 1366
予麋鹿
予麋鹿 2021-01-29 14:38

Task is to check if given string contains balanced sets of {}, [] and ().

For example, check(\"{[}]\") must return

5条回答
  •  太阳男子
    2021-01-29 15:00

    Your solution seems overcomplicated and I have to admit that I didn't try too hard to understand it but it is not obvious to me that it is even correct. A very simple solution to this problem is to create a stack and go through the string pushing opening parentheses to the stack and popping from the stack on closing parentheses (when you pop you have to check that the opening and closing parentheses match).

    It is unfortunately impossible to solve this problem with regular expressions as the language of well balanced parentheses is not regular.

提交回复
热议问题