我想检查用户输入的和的string
数量是否平衡(
)
前任。()(
不平衡
(())
不平衡
def check(string):
counter=0
string=string.replace(" ","")
if string[0] is "(":
for x in string:
if x is "(":
counter=counter+1
elif x is ")":
counter=counter-1
if counter1 is 0:
print("Balanced")
else:
print("Unbalanced")
else:
print ("Unbalanced")
所以这行得通,但我如何用递归解决这个问题?我试图思考每次递归调用它时如何使变量减小,一旦它为0,stop.s