当前位置:网站首页>Bracket matching problem
Bracket matching problem
2022-07-25 09:45:00 【Frost Creek】
The problem is that , We enter a set of strings , These strings are composed of parentheses (,), brackets [,], Curly braces {,} form . The combination is arbitrary . Such as “([]) ",“[()]", "[[({}))]]”“{}(){}[]” And so on are considered to be correct , and “[)","[){","()[}" And so on are considered to be wrong .
To sum up, that is :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order
Code implementation :
def judge(s):L=[]index=0Balance=Trueopenss="([{"closess=")]}"while index<len(s) and Balance:closes=s[index]if closes in "({[":L.append(s[index])else:if len(L)==0:Balance=Falseelse:opens=L.pop()Balance=openss.index(opens)==closess.index(closes)index=index+1if len(L)==0 and Balance:return Trueelse:return False
Example :
str1="([}"print(judge(str1))str2="([{}])"print(judge(str2))
result :
FalseTrue
边栏推荐
猜你喜欢

OC--Foundation--字符串+日期和时间

文件--初识

How to obtain location information (longitude and latitude) by uni app

Some operations of main function

How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)

OC--对象复制

初识Opencv4.X----均值滤波

对象初始化

Kotlin basic knowledge points

CoreData存储待办事项
随机推荐
Student management system (summary)
A picture explains SQL join left and right
Use of map () function in JS
Redis list structure command
关于C和OC
相机姿态估计
OC--Foundation--字符串+日期和时间
OC -- Foundation -- array
OC -- object replication
微信小程序初步了解及实现底部导航栏
OC--继承和多态and指针
How to write Android switching interface with kotlin
chmod和chown对挂载的分区的文件失效
基于机智云平台的温湿度和光照强度获取
如何安装pytorch?—— 一种最简单有效的方法!
Create personal extreme writing process - reprint
Kotlin realizes file download
@4-1 CCF 2020-06-1 linear classifier
OC -- Foundation -- string + date and time
expect+sh实现自动交互