在英文中,有一些标点符号需要成对使用,达到闭合的效果。例如双引号("") 大括号({}) 方括号([])
现在我们需要检测指定文本中的 双引号,大括号, 方括号是否闭合
由若干字母,空格,标点符号组合而成的长度为N, (0<= N <1000)的字符串
双引号,大括号, 方括号 都闭合,返回 true ; 否则返回false;
"I like apple!"
true
I want to go to the zoo [ the small one
false
"{}"true
s=input().strip()
t=[]
for i in range(len(s)):
f=0
if s[i]!=')' or s[i]!='}' or s[i]!=']':
t.append(s[i])
elif s[i]==')':
while t[-1]!='(' and len(t)>0:
if t[-1]=='{' or t[-1]=='[':
f=1
break
t.pop()
t.pop()
elif s[i]=='}':
while t[-1]!='{' and len(t)>0:
if t[-1]=='(' or t[-1]=='[':
f=1
break
t.pop()
t.pop()
else:
while t[-1]!='[' and len(t)>0:
if t[-1]=='(' or t[-1]=='{':
f=1
break
t.pop()
t.pop()
if f==1:break
if '"' in t:
if t.count('"')%2 !=0:f=1
if f==1 or ('[' in t) or ('{' in t) or ('(' in t):print('false')
else:print('true')