原题链接在这里: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. ubuntn18 docker zabbix+grafana安装和使用

    在ubuntu docker inflxudb(安装 使用 备份 还原 以及python编码) telegraf Grafana我采用telegraf采集数据, 本文计划采用zabbix来才采集数据, ...

  2. Ubuntu18 安装搭建Harbor

    1.安装docker-compose1.下载docker-compose的最新版本 sudo curl -L "https://github.com/docker/compose/relea ...

  3. Android -- SEGV_MAPERR,SEGV_ACCERR

    Per siginfo.h: SEGV_MAPERR means you tried to access an address that doesn’t map to anything. SEGV_A ...

  4. golang 使用 protobuf 的教程

    1.下载protobuf的编译器protoc 地址: https://github.com/google/protobuf/releases window:    下载: protoc-3.3.0-w ...

  5. VS 安装resharper 后 无法进行UnitTest

    Vs安装 Resharper后,无法进行单元测试,发现报错提示信息如下: ignored test-case is missing. rebuild the project and try again ...

  6. 分布式系统根基:物理时钟和Lamport逻辑时钟

    分布式系统解决了传统单体架构的单点问题和性能容量问题,另一方面也带来了很多的问题,其中一个问题就是多节点的时间同步问题:不同机器上的物理时钟难以同步,导致无法区分在分布式系统中多个节点的事件时序.19 ...

  7. Microsoft SQL Server数据库语法

    目录   关于数据库的语法: 1.创建数据库 create database 数据库名on primary(主文件属性(name,filename,size等)) -用逗号隔开次要主要文件和次要文件( ...

  8. Django:RestFramework之-------路由

    11.路由 路由设置: url(r'^(?P<version>[v1|v2]+)/vview\.(?P<format>\w+)$', views.VView.as_view({ ...

  9. Vue – 基础学习(3):$forceUpdate()和$nextTick()的区别

    Vue – 基础学习(3):$forceUpdate()和$nextTick()的区别

  10. 解决kettle在两个mysql之间迁移数据时乱码的问题 和 相关报错 及参数调整, 速度优化

    1. 乱码问题 编辑目标数据库的链接: 配置编码参数即可. 2. 报错 No operations allowed after statement closed. 需要调整wait_timeout:  ...