当前位置:网站首页>301. Delete invalid brackets
301. Delete invalid brackets
2022-07-28 11:45:00 【Sun_ Sky_ Sea】
301. Remove invalid brackets
Original title link :https://leetcode.cn/problems/remove-invalid-parentheses/
Here's a string of parentheses and letters s , Remove the minimum number of invalid parentheses , Make the input string valid .
Returns all possible results . You can press In any order return .
Example 1:
Input :s = “()())()”
Output :[“(())()”,“()()()”]
Example 2:
Input :s = “(a)())()”
Output :[“(a())()”,“(a)()()”]
Example 3:
Input :s = “)(”
Output :[“”]
Tips :
1 <= s.length <= 25
s It consists of lowercase English letters and parentheses ‘(’ and ‘)’ form
s At most 20 A bracket
Their thinking :
First count the number of redundant left and right parentheses , Then recursively delete the extra parentheses , Remaining reservations .
Code implementation :
class Solution:
def removeInvalidParentheses(self, s: str) -> List[str]:
# Statistics s The superfluous ( That is, the left and right do not match ) Number of left and right parentheses
l = r = 0
for c in s:
if c == '(':
l += 1
elif c == ')':
if l:
l -= 1
else:
r += 1
ans = []
@lru_cache(None)
def dfs(idx, cur_left, cur_right, del_left, del_right, path):
# Recursive function dfs: from idx Position start , Delete redundant left 、 Close the parenthesis or keep the current character
# cur_left,cur_right Respectively, count the current number of left and right parentheses
# del_left,del_right It is to count the number of left and right parentheses deleted at present
# If the traversed position and s Equal length , That is, there are no redundant left and right parentheses
if idx == len(s):
if not del_left and not del_right:
ans.append(path)
return
# If you want the current right bracket to be more than the left bracket to be deleted , Or the number of left and right parentheses to be deleted is less than 0
if cur_right > cur_left or del_left < 0 or del_right < 0:
return
c = s[idx]
# First delete the redundant number of left and right parentheses counted by the previous function
if c == '(':
dfs(idx + 1, cur_left, cur_right, del_left - 1, del_right, path)
elif c == ')':
dfs(idx + 1, cur_left, cur_right, del_left, del_right - 1, path)
# Recurse to the next position
dfs(idx + 1, cur_left + int(c == '('), cur_right + int(c == ')'), del_left, del_right, path + c)
dfs(0, 0, 0, l, r, "")
return ans
reference :
https://leetcode.cn/problems/remove-invalid-parentheses/solution/python-ji-yi-hua-dfs-by-himymben-5b18/
边栏推荐
- Unity鼠标带动物体运动的三种方法
- Let me think about Linear Algebra: a summary of basic learning of linear algebra
- Object to object mapping -automapper
- Shell (II)
- The fifth generation verification code of "cloud centered, insensitive and extremely fast" is coming heavily
- 移动端人脸风格化技术的应用
- 天狼星网络验证源码/官方正版/内附搭建教程
- Thinkphp5 behavior hook return result (data) example
- Iterative method for determinant (linear algebraic formula)
- Flash point list of cross platform efficiency software
猜你喜欢

b2子主题/博客b2child子主题/开源源码

Sirius network verification source code / official genuine / included building tutorial

Server online speed measurement system source code

使用 Terraform 在 AWS 上快速部署 MQTT 集群

Software testing and quality learning notes 1 --- black box testing

Random talk on GIS data (V) - geographic coordinate system

CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总

Dialogue with Zhuang biaowei: the first lesson of open source

Learning notes tree array

保障邮箱安全,验证码四个优势
随机推荐
Database advanced learning notes - storage structure
Xiaoshuidi 2.0 website navigation network template
How to effectively implement a rapid and reasonable safety evacuation system in hospitals
一种比读写锁更快的锁,还不赶紧认识一下
Rongyun IM & RTC capabilities on new sites
WPF dependent attribute (WPF dependent attribute)
STM32 drives st7701s chip (V ⅰ V0 mobile phone screen change price)
R language ggplot2 visualization: use the ggboxplot function of ggpubr package to visualize the box diagram and customize the fill parameter to configure the filling color of the box
PHP检测url网址链接是否正常可访问
Introduction to web security RADIUS protocol application
Thinkphp5行为钩子Hook返回结果(数据)示例
【一知半解】零值拷贝
Postgres overview
Flash point list of cross platform efficiency software
ripro9.0修正升级版+WP两款美化包+稀有插件
Leecode8 string conversion integer (ATOI)
Refresh your understanding of redis cluster
Five Ali technical experts have been offered. How many interview questions can you answer
A lock faster than read-write lock. Don't get to know it quickly
Database advanced learning notes cursor