当前位置:网站首页>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
边栏推荐
- What is cerebral fissure?
- The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
- Android 如何使用adb命令查看应用本地数据库
- How to write Android switching interface with kotlin
- OC--对象复制
- Create personal extreme writing process - reprint
- Prim minimum spanning tree (diagram)
- OC--Foundation--集合
- App的生命周期和AppleDelegate,SceneDelegate
- Install MySQL in Ubuntu and create new users
猜你喜欢

~1 CCF 2022-06-2 treasure hunt! Big adventure!

OC--继承和多态and指针

STM32+HC05串口蓝牙设计简易的蓝牙音箱

OC--包装类和处理对象

如何将其他PHP版本添加到MAMP

Class (2) and protocol

Prim minimum spanning tree (diagram)

The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do

相机姿态估计

How to convert object data into arrays
随机推荐
@3-1 CCF 2020-09-1 scale detection point query
[code source] I have a big head for a problem every day
【数据挖掘】第二章 认识数据
Kotlin collaboration: foundation and use of collaboration
*7-2 CCF 2015-09-2 date calculation
@3-2 optimal threshold of CCF 2020-12-2 final forecast
Laravel calls a third party to send mail (PHP)
Kotlin basic knowledge points
Redis database foundation
OC--对象复制
初识Opencv4.X----图像直方图绘制
*6-1 CCF 2015-03-2 numerical sorting
STM32+HC05串口蓝牙设计简易的蓝牙音箱
How many regions can a positive odd polygon be divided into
Voice chat app source code - produced by NASS network source code
Data control language (DCL)
Esp8266的Flash读写操作以及Flash上传文件
[code source] daily one question three paragraph
[code source] National Railway
A picture explains SQL join left and right