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

题目:

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.

题解:

From left to right, accoulate left open parenthese.

When encountering ")" and there is no more left "(", then this should be ignored.

Do the same thing from right left.

Time Complexity: O(n). n = s.length().

Space: O(n).

AC Java:

 class Solution {
public String minRemoveToMakeValid(String s) {
if(s == null || s.length() == 0){
return s;
} StringBuilder sb = new StringBuilder();
int left = 0;
for(char c : s.toCharArray()){
if(c == '('){
left++;
}else if(c == ')'){
if(left == 0){
continue;
} left--;
} sb.append(c);
} StringBuilder res = new StringBuilder();
for(int i = sb.length()-1; i>=0; i--){
char c = sb.charAt(i);
if(c == '(' && left-- > 0){
continue;
} res.append(c);
} return res.reverse().toString();
}
}

类似Remove Invalid ParenthesesMinimum Add to Make Parentheses Valid.

LeetCode 1249. Minimum Remove to Make Valid Parentheses的更多相关文章

  1. 【leetcode】1249. Minimum Remove to Make Valid Parentheses

    题目如下: Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the min ...

  2. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. LeetCode第[20]题(Java):Valid Parentheses

    题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  5. LeetCode 20:有效的括号 Valid Parentheses

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...

  6. LeetCode 20. 有效的括号(Valid Parentheses )

    题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字 ...

  7. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  8. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. CentOS修改ulimit

    一.概述 查看limit配置 # ulimit -a core data seg size (kbytes, -d) unlimited scheduling priority (-e) file s ...

  2. 深入理解 Linux Cgroup 系列(一):基本概念

    原文链接:深入理解 Linux Cgroup 系列(一):基本概念 Cgroup 是 Linux kernel 的一项功能:它是在一个系统中运行的层级制进程组,你可对其进行资源分配(如 CPU 时间. ...

  3. 客观->感官->意识->语言->思维->世界观、科学->思想

    客观->感官->意识->语言->思维->世界观.科学->思想

  4. 安装和启动docker

    1.安装和启动docker yum update -y yum install -y yum-utils yum-config-manager --add-repo https://download. ...

  5. MongoDB netcore

    mongodb.driver mongodb.driver.core url:  http://dl.mongodb.org/dl/win32/x86_64 ********************* ...

  6. 【3.1】学习C++之再逢const

    随着学习的深入,就会发现曾经学的const还有更深入的用法,现在就对const的未总结的用法进行总结. 本文就是针对const在类中的情况进行的总结. 有时我们会遇到下面这种将类的成员变量用const ...

  7. 米尔科技MPSoC开发板评测

    米尔科技推出的MYD-CZU3EG开发板搭载的就是UltraScale+ MPSoC平台器件 — XCZU3EG,它集成了四核Cortex-A53 处理器,双核 Cortex-R5 实时处理单元以及M ...

  8. nodeType属性在vue源码中的使用

    每个节点都有一个 nodeType 属性,用于表明节点的类型,节点类型由 Node 类型中定义12个常量表示:  nodeType在vue中的应用 在vue编译的过程中需要查找html结构中的双大括号 ...

  9. Python - 实现矩阵转置

    有个朋友提出了一个问题:手头上现在有一个二维列表,比如[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],现在要把该二维列表变成为[[1, 4, 7, 10 ...

  10. SYN泛洪攻击原理及防御

    拒绝服务攻击时,攻击者想非法占用被攻击者的一些资源,比如如:带宽,CPU,内存等等,使得被攻击者无法响应正常用户的请求. 讲泛洪攻击之前,我们先了解一下DoS攻击和DDoS攻击,这两个攻击大体相同,前 ...