当前位置:网站首页>Simulation volume leetcode [general] 1249 Remove invalid parentheses
Simulation volume leetcode [general] 1249 Remove invalid parentheses
2022-07-06 06:18:00 【Encounter simulation volume】
1249. Remove invalid brackets
Here you are ‘(’、‘)’ And a string of lowercase letters s.
You need to remove the minimum number of entries from the string ‘(’ perhaps ‘)’ ( You can remove brackets anywhere ), Make the rest 「 Bracket string 」 It works .
Please return any legal string .
It works 「 Bracket string 」 It should conform to the following Any one of them requirement :
Empty strings or strings containing only lowercase letters
Can be written AB(A Connect B) String , among A and B It's all effective 「 Bracket string 」
Can be written (A) String , among A Is an effective 「 Bracket string 」
Example 1:
Input :s = “lee(to)de)”
Output :“lee(to)de”
explain :“lee(t(co)de)” , “lee(tode)” It's also a possible answer .
Example 2:
Input :s = “a)bd”
Output :“abd”
Example 3:
Input :s = “))((”
Output :“”
explain : Empty strings are also valid
Example 4:
Input :s = “(a(bd)”
Output :“a(bd)”
Tips :
1 <= s.length <= 10^5
s[i] May be ‘(’、‘)’ Or English lowercase letters
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-remove-to-make-valid-parentheses
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def minRemoveToMakeValid(self, s: str) -> str:
left,right = 0,s.count(')')
res = ''
for char in s:
print(char,left,right)
if char=='(':
if right>0:
res += char
left+=1
right-=1
elif char==')':
if left>0:
res += char
left-=1
else:
right-=1
else:
res +=char
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list turn node
return s.minRemoveToMakeValid(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
["lee(t(c)o)de)"],
["))(("],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- PAT(乙级)2022年夏季考试
- [eolink] PC client installation
- 黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
- Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- Selenium source code read through · 9 | desiredcapabilities class analysis
- Seven imperceptible truths in software testing
- 【C语言】qsort函数
- How to extract login cookies when JMeter performs interface testing
- Amazon Engineer: eight important experiences I learned in my career
猜你喜欢
Isam2 operation process
MySQL之基础知识
Selenium source code read through · 9 | desiredcapabilities class analysis
F - true liars (category and search set +dp)
P问题、NP问题、NPC问题、NP-hard问题详解
Postman核心功能解析-参数化和测试报告
MFC关于长字符串unsigned char与CString转换及显示问题
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
Nodejs realizes the third-party login of Weibo
随机推荐
F - True Liars (种类并查集+DP)
联合索引的左匹配原则
Embedded point test of app
D - How Many Answers Are Wrong
【API接口工具】postman-界面使用介绍
数学三大核心领域概述:代数
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
MFC关于长字符串unsigned char与CString转换及显示问题
数据库隔离级别
使用Nacos管理配置
Nodejs realizes the third-party login of Weibo
[C language] qsort function
Luogu p1460 [usaco2.1] healthy Holstein cows
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
【Postman】动态变量(也称Mock函数)
G - Supermarket
全链路压测:构建三大模型
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
Customize the gateway filter factory on the specified route