Given a String with parentheses, return a string with balanced parentheses by removing fewest characters possible. You cannot add anything to the string.
E.g.
balabce(“()”) -> “()”
balance(“)(“) -> “”
balance(“(((((()”) -> “()”
balance(“()(((()()”) -> “()()()”
Analysis:
Obviously the requirement of this question is to delete all unclosed parentheses and reserve closed parentheses. At the same time, we need to close a right parenthesis with its nearest left parenthesis.
The idea is like following:- Traverse the string from left to right. Count the number of left and right parenthesis. If the number of right parenthesis is greater than the number of left parenthesis, delete the right parenthesis.
- Traverse the string from right to left. Count the number of left and right parenthesis. If the number of left parenthesis is greater than the number of right parenthesis, delete the left parenthesis.
O(2n) = O(n)time,O(1)space. n is the length of the string.
Java implementation:
public class Solution {
public String balance(String s) {
int l = s.length();
char[] chs = s.toCharArray();
int L = 0;
int R = 0;
for (int i = 0; i < l; i++) {
if (chs[i] == '(') L++;
if (chs[i] == ')') R++;
if (R > L) {
chs[i] = '*';
R--;
}
}
L = 0;
R = 0;
for (int i = l - 1; i >= 0; i--) {
if (chs[i] == ')') R++;
if (chs[i] == '(') L++;
if (L > R) {
chs[i] = '*';
L--;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < l; i++) {
if (chs[i] != '*') {
sb.append(String.valueOf(chs[i]));
}
}
return sb.toString();
}
}

本文介绍了一种算法,用于从含有括号的字符串中删除最少数量的字符以达到括号平衡的目的。该方法通过两次遍历字符串(从左到右及从右到左),确保保留尽可能多的有效括号对。
1万+

被折叠的 条评论
为什么被折叠?



