当前位置:网站首页>LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
2022-04-23 09:50:00 【CP Coding】
Given a string s of '('
, ')'
and lowercase English characters.
Your task is to remove the minimum number of parentheses ( '('
or ')'
, in any positions ) so that the resulting parentheses string is valid and return any valid string.
Formally, a parentheses string is valid if and only if:
- It is the empty string, contains only lowercase characters, or
- It can be written as
AB
(A
concatenated withB
), whereA
andB
are valid strings, or - It can be written as
(A)
, whereA
is a valid string.
Example 1:
Input: s = "lee(t(c)o)de)" Output: "lee(t(c)o)de" Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
Input: s = "a)b(c)d" Output: "ab(c)d"
Example 3:
Input: s = "))((" Output: "" Explanation: An empty string is also valid.
Constraints:
1 <= s.length <= 105
s[i]
is either'('
,')'
, or lowercase English letter.
题目说给定一个字符串包含英文字母和左右小括号'('和‘)’,字符串里的左右小括号有可能是不匹配的,要求移除最少个数的左右小括号使得剩下的是完全匹配的,返回移除后的有效字符串。
首先要搞清楚左右小括号的匹配原则,当遇到一个左括号时,后面必须要有一个右括号才能匹配,要是直到最后也没出现右括号则该左括号为无效需被删除;当遇到一个右括号时,其前面必须要有一个等着匹配的左括号,否则该右括号为无效需要被删除。
由此可见,整个删除过程是基于左括号的,因此可以用一个堆栈来记录待匹配的左括号,从头到尾扫一遍字符串,当遇到左括号就把左括号放入堆栈里(堆栈里放的是当前左括号的位置);当遇到右括号时,就判断此时堆栈是否为空,如不为空则从堆栈顶弹出一个左括号与其匹配,如为空表示该右括号需要被删除。按此方法直到遍历结束,结束后堆栈里剩下的左括号也都是要被删除的。在整个过程中用一个set来记录要被删除的括号的位置,最后再遍历一遍统一删除,这样效率会比较高。
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
rm = set()
st = []
for i,c in enumerate(s):
if c == '(':
st.append(i)
elif c == ')':
if not st:
rm.add(i)
else:
st.pop()
while st:
rm.add(st.pop())
res = []
for i,c in enumerate(s):
if i not in rm:
res.append(c)
return ''.join(res)
版权声明
本文为[CP Coding]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hgq522/article/details/124333962
边栏推荐
- 如何实现根据照片获取地理位置及如何防御照片泄漏地理位置
- Less than 100 secrets about prime numbers
- Redis exception read error on connection solution
- 构建元宇宙时代敏捷制造的九种能力
- 自定义登录失败处理
- AI上推荐 之 MMOE(多任务yyds)
- A concise course of fast Fourier transform FFT
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
- SAP CR transmission request sequence and dependency check
- 面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
猜你喜欢
Planning and construction of industrial meta universe platform
Introduction to sap pi / PO login and basic functions
计算机网络安全实验二|DNS协议漏洞利用实验
Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
Go language learning notes - exception handling | go language from scratch
ABAP publishes OData service samples from CDs view
MySQL of database -- overview and installation
构建元宇宙时代敏捷制造的九种能力
JSON input of Chapter 14 of kettle paoding jieniu
Personal homepage software fenrus
随机推荐
亚马逊云科技入门资源中心,从0到1轻松上云
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
JS DOM learn three ways to create elements
Leetcode0587. Install fence
JS scope, scope chain, global variables and local variables
[hdu6868] absolute math (pusher + Mobius inversion)
Odoo 服务器搭建备忘
Rain produces hundreds of valleys, and all things grow
Two declaration methods of functions of JS
理解作用域
Three ways to create objects in JS
GUI, CLI and UNIX Philosophy
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Redis 内存占满导致的 Setnx 命令执行失败
Alibaba cloud architects interpret the four mainstream game architectures
个人主页软件Fenrus
Introduction to graph theory -- drawing
计算机网络安全实验二|DNS协议漏洞利用实验
"Gu Yu series" airdrop
Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie