-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove outermost parenthesis.java
More file actions
30 lines (27 loc) · 1.29 KB
/
remove outermost parenthesis.java
File metadata and controls
30 lines (27 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public String removeOuterParentheses(String s) {
StringBuilder bracket = new StringBuilder();
int start = 0, balance = 0;
for( int i =0 ; i<s.length(); i++){
if ( s.charAt(i) == '('){
balance++ ;
}
else{
balance-- ;
}
if( balance == 0){
bracket.append(s.substring(start + 1, i));
start = i+1;
}
}
return bracket.toString();
}
}
// The code defines a class Solution with a method removeOuterParentheses that takes a string s as input.
// The method removes the outermost parentheses from each primitive substring of s.
// It uses a StringBuilder to build the result and keeps track of the balance of parentheses.
// The method iterates through the string, updating the balance for each parenthesis encountered.
// When the balance reaches zero, it appends the substring from the start index (excluding the outer parentheses) to the StringBuilder.
// Finally, it returns the resulting string without the outer parentheses.
// The time complexity of this solution is O(n), where n is the length of the input string s.
// The space complexity is O(n) as well, due to the use of StringBuilder to store the result.