有效括号字符串为空 ""、"(" + A + ")" 或 A + B ,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接。
例如,"","()","(())()" 和 "(()(()))" 都是有效的括号字符串。
如果有效字符串 s 非空,且不存在将其拆分为 s = A + B 的方法,我们称其为原语(primitive),其中 A 和 B 都是非空有效括号字符串。
给出一个非空有效字符串 s,考虑将其进行原语化分解,使得:s = P_1 + P_2 + ... + P_k,其中 P_i 是有效括号字符串原语。
对 s 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 s 。 示例 1:
输入:s = "(()())(())"
输出:"()()()"
解释:
输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。
var removeOuterParentheses = function(s) {
const stack = [], n = s.length
let r = ''
for (let i = 0; i < n; i++) {
if (s[i] === ')') stack.pop()
if (stack.length) r += s[i]
if (s[i] === '(') stack.push('(')
}
return r
};
function removeOuterParentheses(s: string): string {
const stack: string[] = [], n: number = s.length
let r: string = ''
for (let i = 0; i < n; i++) {
if (s[i] === ')') stack.pop()
if (stack.length) r += s[i]
if (s[i] === '(') stack.push('(')
}
return r
};
class Solution {
function removeOuterParentheses($s) {
$n = strlen($s);
$stack = [];
$r = '';
for ($i = 0; $i < $n; $i++) {
if ($s[$i] === ')') array_pop($stack);
if (count($stack) > 0) $r .= $s[$i];
if ($s[$i] === '(') array_push($stack, $s[$i]);
}
return $r;
}
}
func removeOuterParentheses(s string) string {
n, stack, b := len(s), []byte{}, &bytes.Buffer{}
for i := 0; i < n; i++ {
if s[i] == ')' {
stack = stack[:len(stack) - 1]
}
if len(stack) > 0 {
b.WriteByte(s[i])
}
if s[i] == '(' {
stack = append(stack, '(')
}
}
return b.String()
}
class Solution {
public String removeOuterParentheses(String s) {
Deque<Character> stack = new ArrayDeque<Character>();
StringBuffer r = new StringBuffer();
char[] chars = s.toCharArray();
for (char c : chars) {
if (c == ')') stack.pop();
if (stack.isEmpty() == false) r.append(c);
if (c == '(') stack.push('(');
}
return r.toString();
}
}
class Solution:
def removeOuterParentheses(self, s: str) -> str:
stack, r = [], ''
for byte in s:
if byte == ')': stack.pop()
if len(stack) > 0: r += byte
if byte == '(': stack.append('(')
return r
var removeOuterParentheses = function(s) {
const n = s.length
let depth = r = ''
for (let i = 0; i < n; i++) {
if (s[i] === ')') depth--
if (depth > 0) r += s[i]
if (s[i] === '(') depth++
}
return r
};
function removeOuterParentheses(s: string): string {
let depth = 0, r = ''
for (const c of s) {
if (c === ')') depth--
if (depth > 0) r += c
if (c === '(') depth++
}
return r
};
class Solution {
function removeOuterParentheses($s) {
$depth = 0;
$r = '';
$n = strlen($s);
for ($i = 0; $i < $n; $i++) {
if ($s[$i] === ')') $depth--;
if ($depth > 0) $r .= $s[$i];
if ($s[$i] === '(') $depth++;
}
return $r;
}
}
func removeOuterParentheses(s string) string {
depth, r := 0, &strings.Builder{}
for _, c := range s {
if c == ')' {
depth--
}
if depth > 0 {
r.WriteRune(c)
}
if c == '(' {
depth++
}
}
return r.String()
}
class Solution {
public String removeOuterParentheses(String s) {
int depth = 0;
StringBuffer r = new StringBuffer();
char[] chars = s.toCharArray();
for (char c : chars) {
if (c == ')') depth--;
if (depth > 0) r.append(c);
if (c == '(') depth++;
}
return r.toString();
}
}
class Solution:
def removeOuterParentheses(self, s: str) -> str:
depth, r = 0, ''
for byte in s:
if byte == ')': depth -= 1
if depth > 0: r += byte
if byte == '(': depth += 1
return r
char * removeOuterParentheses(char * s){
int n = strlen(s);
int depth = 0;
char *r = (char *)malloc(sizeof(char) * (n + 1));
int j = 0;
for (int i = 0; i < n; i++) {
if (s[i] == ')') depth++;
if (depth != 0) r[j++] = s[i];
if (s[i] == '(') depth--;
}
r[j] = '\0';
return r;
}
class Solution {
public:
string removeOuterParentheses(string s) {
int depth = 0;
string r;
for (auto c : s) {
if (c == ')') depth--;
if (depth > 0) r.push_back(c);
if (c == '(') depth++;
}
return r;
}
};
public class Solution {
public string RemoveOuterParentheses(string s) {
int depth = 0;
StringBuilder r = new StringBuilder();
foreach(char c in s) {
if (c == ')') depth--;
if (depth > 0) r.Append(c);
if (c == '(') depth++;
}
return r.ToString();
}
}