原题链接在这里: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. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  2. 【Python爬虫案例学习】Python爬取天涯论坛评论

    用到的包有requests - BeautSoup 我爬的是天涯论坛的财经论坛:'http://bbs.tianya.cn/list.jsp?item=develop' 它里面的其中的一个帖子的URL ...

  3. Android.mk文件官方使用说明

    本页介绍了 ndk-build 所使用的 Android.mk 编译文件的语法. 概览 Android.mk 文件位于项目 jni/ 目录的子目录中,用于向编译系统描述源文件和共享库.它实际上是编译系 ...

  4. 搞清楚一道关于Integer的面试题【华为云技术分享】

    请看题1: public class IntegerDemo { public static void main(String[] args) { Integer a = ; Integer b = ...

  5. 封装:WPF基于MediaElement封装的视频播放器

    原文:封装:WPF基于MediaElement封装的视频播放器 一.目的:应用MediaElement创建媒体播放器 二.效果图 三.目前支持功能 播放.暂停.停止.快进.快退.声音大小.添加播放列表 ...

  6. 类例程_c#战斗程序(窗体版)

    战士类代码: class Fight { String name; int attack, speed, crit, armor;// 生命.攻击力,攻速,暴击,护甲 public int life; ...

  7. Vertx的命令行

    IntelliJ----创建一个运行配置(Application), 用io.vertx.core.Launcher类作为主类,在程序参数输入:run your-verticle-fully-qual ...

  8. xcode模拟器使用常用的命令。

    1.查看模拟器的udid用的 xcrun instruments -s xcrun simctl list 2.启动这个模拟器: xcrun instruments -w 'B39EC2FF-8A8B ...

  9. linux安装php nginx mysql

    linux装软件方式: systemctl status firewalld.service 查看防火墙systemctl stop firewalld.service systemctl disab ...

  10. C# 如何提前结束 Sleep ?

    好久没有更新博客了,都有点对不起这个账号了.这次跟大家分享的是一种编程思路,没什么技术含量,但也许能帮得到你. 我们经常会在程序程序中用到 Sleep 这个方法.Sleep 方法用起来非常简单,但是有 ...