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
(A
concatenated withB
), whereA
andB
are valid strings, or - It can be written as
(A)
, whereA
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:
1 class Solution { 2 public String minRemoveToMakeValid(String s) { 3 if(s == null || s.length() == 0){ 4 return s; 5 } 6 7 StringBuilder sb = new StringBuilder(); 8 int left = 0; 9 for(char c : s.toCharArray()){ 10 if(c == '('){ 11 left++; 12 }else if(c == ')'){ 13 if(left == 0){ 14 continue; 15 } 16 17 left--; 18 } 19 20 sb.append(c); 21 } 22 23 StringBuilder res = new StringBuilder(); 24 for(int i = sb.length()-1; i>=0; i--){ 25 char c = sb.charAt(i); 26 if(c == '(' && left-- > 0){ 27 continue; 28 } 29 30 res.append(c); 31 } 32 33 return res.reverse().toString(); 34 } 35 }
If we use C++, we can directly manipulate on the s and make the space complexity as O(1) regardless res.
Time Complexity: O(n). Space: O(1).
AC C++:
1 class Solution { 2 public: 3 string minRemoveToMakeValid(string s) { 4 int n = s.length(); 5 int left = 0; 6 for(int i = 0; i < n; i++){ 7 if(s[i] == '('){ 8 left++; 9 }else if(s[i] == ')'){ 10 if(left == 0){ 11 s[i] = '#'; 12 }else{ 13 left--; 14 } 15 } 16 } 17 18 int right = 0; 19 for(int i = n - 1; i >= 0; i--){ 20 if(s[i] ==')'){ 21 right++; 22 }else if(s[i] == '('){ 23 if(right == 0){ 24 s[i] = '#'; 25 }else{ 26 right--; 27 } 28 } 29 } 30 31 string res = ""; 32 for(char c : s){ 33 if(c == '#'){ 34 continue; 35 } 36 37 res += c; 38 } 39 40 return res; 41 } 42 };
AC Python:
1 class Solution: 2 def minRemoveToMakeValid(self, s: str) -> str: 3 n = len(s) 4 s_list = list(s) 5 left = 0 6 for i in range(n): 7 if s_list[i] =='(': 8 left += 1 9 elif s_list[i] ==')': 10 if left == 0: 11 s_list[i] = '' 12 else: 13 left -= 1 14 15 right = 0 16 for i in range(n - 1, -1, -1): 17 if s_list[i] == ')': 18 right += 1 19 elif s_list[i] == '(': 20 if right == 0: 21 s_list[i] = '' 22 else: 23 right -= 1 24 25 return ''.join(s_list) 26 27
类似Remove Invalid Parentheses, Minimum Add to Make Parentheses Valid, Minimum Number of Swaps to Make the String Balanced, Check if a Parentheses String Can Be Valid.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步