1021. Remove Outermost Parentheses
/**
* Problem: LeetCode 1021 - Remove Outermost Parentheses
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public String removeOuterParentheses(String s) {
StringBuilder sb = new StringBuilder();
int depth = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
if (depth > 0) sb.append(c);
depth++;
} else {
depth--;
if (depth > 0) sb.append(c);
}
}
return sb.toString();
}
}
"""
Problem: LeetCode 1021 - Remove Outermost Parentheses
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
class Solution:
def removeOuterParentheses(self, s: str) -> str:
res = []
depth = 0
for c in s:
if c == '(':
if depth > 0:
res.append(c)
depth += 1
else:
depth -= 1
if depth > 0:
res.append(c)
return "".join(res)
/**
* Problem: LeetCode 1021 - Remove Outermost Parentheses
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <string>
using namespace std;
/**
* Problem: LeetCode 1021 - Remove Outermost Parentheses
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
string removeOuterParentheses(string s) {
string result = "";
int depth = 0;
for (char c : s) {
if (c == '(') {
if (depth > 0) result += c;
depth++;
} else {
depth--;
if (depth > 0) result += c;
}
}
return result;
}
};
/**
* Problem: LeetCode 1021 - Remove Outermost Parentheses
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var removeOuterParentheses = function(s) {
let result = '';
let depth = 0;
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (c === '(') {
if (depth > 0) result += c;
depth++;
} else {
depth--;
if (depth > 0) result += c;
}
}
return result;
};