检查Python中的平衡括号
给定一个表达式字符串,编写一个Python程序来查找给定的字符串是否具有平衡括号。
例子:
Input : {[]{()}}
Output : Balanced
Input : [{}{}(]
Output : Unbalanced
方法#1:使用堆栈
检查平衡括号的一种方法是使用堆栈。每次遇到开括号时,将其压入堆栈,遇到闭括号时,将其与堆栈顶部匹配并弹出。如果栈最后是空的,则返回Balanced,否则返回Unbalanced。
# Python3 code to Check for
# balanced parentheses in an expression
open_list = ["[","{","("]
close_list = ["]","}",")"]
# Function to check parentheses
def check(myStr):
stack = []
for i in myStr:
if i in open_list:
stack.append(i)
elif i in close_list:
pos = close_list.index(i)
if ((len(stack) > 0) and
(open_list[pos] == stack[len(stack)-1])):
stack.pop()
else:
return "Unbalanced"
if len(stack) == 0:
return "Balanced"
else:
return "Unbalanced"
# Driver code
string = "{[]{()}}"
print(string,"-", check(string))
string = "[{}{})(]"
print(string,"-", check(string))
string = "((()"
print(string,"-",check(string))
输出:
{[]{()}} - Balanced
[{}{})(] - Unbalanced
((() - Unbalanced
方法#2:使用队列
首先将左括号映射到相应的右括号。使用 'i' 遍历给定的表达式,如果 'i' 是左括号,则追加到队列中,如果 'i' 是右括号,检查队列是否为空或 'i' 是队列的顶部元素,如果是,返回“不平衡”,否则“平衡”。
# Python3 code to Check for
# balanced parentheses in an expression
def check(expression):
open_tup = tuple('({[')
close_tup = tuple(')}]')
map = dict(zip(open_tup, close_tup))
queue = []
for i in expression:
if i in open_tup:
queue.append(map[i])
elif i in close_tup:
if not queue or i != queue.pop():
return "Unbalanced"
if not queue:
return "Balanced"
else:
return "Unbalanced"
# Driver code
string = "{[]{()}}"
print(string, "-", check(string))
string = "((()"
print(string,"-",check(string))
输出:
{[]{()}} - Balanced
((() - Unbalanced
方法#3:基于消除
在每次迭代中,最里面的括号都会被消除(替换为空字符串)。如果我们最终得到一个空字符串,那么我们最初的字符串是平衡的;否则,不是。
# Python3 code to Check for
# balanced parentheses in an expression
def check(my_string):
brackets = ['()', '{}', '[]']
while any(x in my_string for x in brackets):
for br in brackets:
my_string = my_string.replace(br, '')
return not my_string
# Driver code
string = "{[]{()}}"
print(string, "-", "Balanced"
if check(string) else "Unbalanced")
输出:
{[]{()}} - Balanced