题目如下:

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 with B), where A and B are valid strings, or
  • It can be written as (A), where A 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.

Example 4:

Input: s = "(a(b(c)d)"
Output: "a(b(c)d)"

Constraints:

  • 1 <= s.length <= 10^5
  • s[i] is one of  '(' , ')' and lowercase English letters.

解题思路:本题不难,和以前很多括号问题解法类似。从头开始遍历s,如果为左括号,记录其下标,存入数组 left_unpaired中;如果是右括号,找到与其最近的左括号,并将对应左括号从left_uncompair删除,如果没有最近的括号,将右括号存入 right_unpaired中。最后,只要删掉所有存在于 left_unpaired或 right_unpaired中的括号即可。

代码如下:

class Solution(object):
def minRemoveToMakeValid(self, s):
"""
:type s: str
:rtype: str
"""
left_uncompair = []
left_remove = {}
right_remove = {}
for i in range(len(s)):
if s[i] == '(':
left_uncompair.append(i)
left_remove[i] = 1
elif s[i] == ')':
if len(left_uncompair) <= 0:
right_remove[i] = 1
else:
inx = left_uncompair.pop(-1)
del left_remove[inx] res = ''
for i in range(len(s)):
if i in right_remove or i in left_remove:
continue
res += s[i]
return res

【leetcode】1249. Minimum Remove to Make Valid Parentheses的更多相关文章

  1. LeetCode 1249. Minimum Remove to Make Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 题目: Given a string s ...

  2. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  3. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  4. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  5. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  6. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  7. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  8. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...

  9. 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

随机推荐

  1. C学习笔记-内存管理

    作用域 一个C语言变量的作用域可以是代码块 作用域,函数作用域或者文件作用域 代码块是{}之间的一段代码 同一个代码块不可以有重名变量 auto自动变量 一般情况下代码块内部定义的变量都是自动变量 也 ...

  2. CentOS配置java环境,mysql数据库等文章链接

    配置jdk 配置jdk 安装mysql8 yum install -y mysql-community-server 安装mysql8 安装redi 安装redis 安装docker 安装docker

  3. 自己总结的keepalived的配置流程以及注意事项

    编写背景:上班时领导要求我们团队实现postgresql主备切换的高可用问题,我辅助keepalived的部分,从查资料到实施最后使用,最后编写了这个博客,水平有限,欢迎大家指正 ###postgre ...

  4. priority_queue member function

    没有优先队列的dijkstra不算真的dijkstra 所以我又回来补常识了 <1>priority_queue::emplace <7>priority_queue::top ...

  5. 坦克大战--Java类型 ---- (3)实现socket通信

    一.实现思路 使用socket通信的一些方法来实现socket通信,客户端和服务端两边需要约定好通信的接口Port(尽量选高的),客户端需要服务端的IP地址,以实现数据交流. 同时,客户端和服务端需要 ...

  6. java执行bat代码

    java执行bat代码.txt public static void runbat(String path,String filename) { String cmd = "cmd /c s ...

  7. LintCode 29---交叉字符串

    public class Solution { /** * @param s1: A string * @param s2: A string * @param s3: A string * @ret ...

  8. debezium关于cdc的使用(下)

    博文原址:debezium关于cdc的使用(下) 简介 debezium在debezium关于cdc的使用(上)中有做介绍.具体可以跳到上文查看.本篇主要讲述使用kafka connector方式来同 ...

  9. 10 Python之文件操作

    1.文件操作 f = open(文件路径, mode="模式", encoding="编码") f: 文件句柄 文件的路径: 相对路径 相对于当前程序所在的文件 ...

  10. HTTPS中SSL/TLS握手时的私钥用途(RSA、ECDHE)

    从上一篇HTTPS中CA证书的签发及使用过程中知道服务端在申请CA证书时只上交了密钥对中的公钥,那么只有服务端知道的私钥有什么作用呢? SSL/TLS层的位置 SSL/TLS层在网络模型的位置,它属于 ...