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<Pair<Character, Integer>> st = new Stack<>();
 5         for (int i = 0; i < sb.length(); ++i) {
 6             if (sb.charAt(i) == '(') {
 7                 st.add(new Pair<>('(', i));  
 8             } else if (sb.charAt(i) == ')') {
 9                 if (!st.empty() && st.peek().getKey() == '(') {
10                     st.pop();
11                 } else {
12                     st.add(new Pair<>(')', i));
13                 }
14             }
15         }
16         while (!st.empty()) {
17             sb.deleteCharAt(st.pop().getValue());
18         }
19         return sb.toString();
20     }
21 }
we need the closecount to know whehter we should print the left bracket or not.
we need the openCount to decide how many open brackets have been visited and no pair yet and we need it when we encouter closing bracket and decide whether we should print it or not.
 1 class Solution {
 2     public String minRemoveToMakeValid(String s) {
 3         int openCount = 0; // count of un-matched open bracket
 4         int closeCount = 0; // count of un-matched close bracket 
 5         for (int i = 0; i < s.length(); i++) {
 6             if (s.charAt(i) == ')') {
 7                 closeCount++;
 8             }
 9         }
10 
11         StringBuilder sb = new StringBuilder();
12 
13         for (int i = 0; i < s.length(); i++) {
14             char c = s.charAt(i);
15             if (c == '(') {
16                 if (openCount < closeCount) {
17                     openCount++;
18                     sb.append(c);
19                 }
20             } else if (c == ')') {
21                 closeCount--;
22                 if (openCount > 0) {
23                     sb.append(c);
24                     openCount--;
25                 }
26             } else {
27                 sb.append(c);
28             }
29         }
30         return sb.toString();
31     }
32 }

 

posted @ 2020-02-06 13:46  北叶青藤  阅读(170)  评论(0)    收藏  举报