LeetCode 1249. Minimum Remove to Make Valid Parentheses
原题链接在这里: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(Aconcatenated withB), whereAandBare valid strings, or - It can be written as
(A), whereAis 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^5s[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 Parentheses, Minimum Add to Make Parentheses Valid.
LeetCode 1249. Minimum Remove to Make Valid Parentheses的更多相关文章
- 【leetcode】1249. Minimum Remove to Make Valid Parentheses
题目如下: Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the min ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...
- LeetCode 20. 有效的括号(Valid Parentheses )
题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字 ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- [更新] Jetbrains IntelliJ IDEA 2019 的许可证密钥
亲测有效.转载目的:提供一种方法,当然可以看一下原网站的更新及其他方法.转载:[更新] Jetbrains IntelliJ IDEA 2019 的许可证密钥(100%工作),By Gideon ...
- tomcat8 url包含|等特殊字符报错400的问题
这个问题纠缠了我很久了,终于在今天早上解决了,感谢自己的不放弃和不断尝试的决心,我坚信,我可以找到解决方式!! 项目用的spring .spring mvc.hibernate框架,关于统一错误页面在 ...
- 实现SpringCloud Config 客户端自动刷新
文章来源:https://blog.csdn.net/qq_27385301/article/details/82716218 一.简介 在使用SpringCloud Config客户端时,如果Con ...
- Java 字符串(二)字符串常用操作
一.连接字符串 1.连接多个字符串 使用“+”运算符可以实现连接多个字符串的功能.“+” 运算符可以连接多个运算符并产生一个 String 对象. 2.连接其他数据类型 字符串与其他基本数据类型进行连 ...
- Python学习日记(三十八) Mysql数据库篇 六
Mysql视图 假设执行100条SQL语句时,里面都存在一条相同的语句,那我们可以把这条语句单独拿出来变成一个'临时表',也就是视图可以用来查询. 创建视图: CREATE VIEW passtvie ...
- Docker搭建Adminer(数据库图形化管理界面)
1.下载官方库的adminer镜像: docker pull adminer 2.创建adminer容器:docker run --link mysql:mysql --name adminer -p ...
- Python xlrd模块读取Excel表中的数据
1.xlrd库的安装 直接使用pip工具进行安装(当然也可以使用pycharmIDE进行安装,这里就不详述了) pip install xlrd 2.xlrd模块的一些常用命令 ①打开excel文件并 ...
- K8S概念理解参考
- 树莓派安装realvnc_server
先 sudo raspi-config 打开VNC. 然后去realvnc官网去下载raspberry的vncserver 已经ssh连接的前提下可以电脑下载后使用scp命令转移到树莓派上,使用以下命 ...
- 100% 成功率的 offer 收割机是怎样练成的?
都说今年的形势不好,各种找工作不顺利,但我身边就有一位同学,每次面试都拿到offer,我特意邀请他来给大家分享下经验,虽然不同人的技术领域未必相同,但很多东西是相通的,希望本文能对大家有所帮助. 下面 ...