1249. 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)"

分析:用一个stack来记录需要删除的括号,最后把它们删除了就可以了。
 1 class Solution {
 2     public String minRemoveToMakeValid(String s) {
 3       StringBuilder sb = new StringBuilder(s);
 4       Stack<Integer> st = new Stack();
 5       for (int i = 0; i < sb.length(); ++i) {
 6         if (sb.charAt(i) == ‘(‘) {
 7             st.add(i + 1);
 8         } else if (sb.charAt(i) == ‘)‘) {
 9           if (!st.empty() && st.peek() > 0) {
10               st.pop();
11           } else {
12               st.add(-(i + 1));
13           }
14         }
15       }
16       while (!st.empty()) {
17         sb.deleteCharAt(Math.abs(st.pop()) - 1);
18       }
19       return sb.toString();
20     }
21 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/12268458.html

时间: 2024-08-30 16:53:48

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 minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a

Java [leetcode 32]Longest Valid Parentheses

题目描述: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another exampl

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e

LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间

题目: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example