s = '(]{)[{(})}'
# s = '()'
if len(s) % 2 == 1:
print('字符串个数为奇数,无法匹配')
else:
pairs = {
")": "(",
"]": "[",
"}": "{",
}
stack = list()
for ch in s:
# print(ch)
if ch in pairs:
# print('当前元素:', ch)
# print('stack内的元素:', stack)
if not stack or stack[-1] != pairs[ch]:
print('当前字符串不满足括号匹配!')
break
stack.pop()
else:
stack.append(ch)
print('stack:', stack)
if not stack:
print(s, '字符串满足括号匹配!')
06-22
169
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交