当前位置:网站首页>LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
2022-04-23 10:12: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.
The title says that a given string contains English letters and left and right parentheses '(' and ‘)’, The left and right parentheses in the string may not match , It is required to remove the minimum number of left and right parentheses so that the rest are exactly matched , Returns a valid string after removal .
First, find out the matching principle of the left and right parentheses , When you encounter an open parenthesis , You must have a closing parenthesis after it to match , If the right bracket does not appear until the end, the left bracket is invalid and needs to be deleted ; When a closing bracket is encountered , It must be preceded by an open parenthesis waiting to match , Otherwise, the closing bracket is invalid and needs to be deleted .
thus it can be seen , The whole deletion process is based on the left parenthesis , Therefore, you can use a stack to record the left parenthesis to be matched , Scan the string from beginning to end , When you encounter an open bracket, put the open bracket on the stack ( In the stack is the position of the current left parenthesis ); When you encounter a close bracket , Just judge whether the stack is empty at this time , If it is not empty, an open bracket will pop up from the top of the stack to match it , If it is empty, it means that the closing bracket needs to be deleted . Follow this method until the end of traversal , After that, the remaining left parentheses in the stack will also be deleted . Use one throughout the process set To record the position of the brackets to be deleted , Finally, go through it again and delete it uniformly , This will be more efficient .
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://yzsam.com/2022/04/202204230949469493.html
边栏推荐
- DBA常用SQL语句(4)- Top SQL
- Custom login failure handling
- Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
- 构建元宇宙时代敏捷制造的九种能力
- Juc并发编程09——Condition实现源码分析
- JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
- Sim Api User Guide(5)
- 中职网络安全2022国赛之CVE-2019-0708漏洞利用
- Formattime timestamp format conversion
- Sim Api User Guide(7)
猜你喜欢
failureForwardUrl与failureUrl
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
实践六 Windows操作系统安全攻防
Custom login failure handling
0704、ansible----01
Configuration of LNMP
Sim Api User Guide(5)
Solve the problem of installing VMware after uninstalling
C语言——自定义类型
随机推荐
DBA common SQL statements (2) - SGA and PGA
DBA常用SQL语句(2)— SGA和PGA
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
MapReduce核心和基础Demo
Jerry's factors that usually affect CPU performance test results are: [article]
《Redis设计与实现》
Ansible cloud computing automation
中控学习型红外遥控模块支持网络和串口控制
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
242、有效字母异位词(哈希表)
域名和IP地址的联系
Common SQL statements of DBA (6) - daily management
CSP认证 202203-2 出行计划(多种解法)
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
"Gu Yu series" airdrop
DBA常用SQL语句 (5) - Latch 相关
454、四数之和(哈希表)
通过流式数据集成实现数据价值(2)
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
2022茶艺师(初级)考试试题模拟考试平台操作