Skip to content

Commit 87975d3

Browse files
solves remove outermost parantheses
1 parent 45a5f1d commit 87975d3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
| 1010 | [Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60) | [![Java](assets/java.png)](src/PartitionArrayIntoThreePartsWithEqualSum.java) | |
276276
| 1013 | [Partition Array into Three Parts with equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum) | | |
277277
| 1018 | [Binary Prefix Divisible by 5](https://leetcode.com/problems/binary-prefix-divisible-by-5) | [![Java](assets/java.png)](src/BinaryPrefixDivisibleBy5.java) | |
278-
| 1021 | [Remove Outermost Parenthesis](https://leetcode.com/problems/remove-outermost-parentheses) | | |
278+
| 1021 | [Remove Outermost Parenthesis](https://leetcode.com/problems/remove-outermost-parentheses) | [![Java](assets/java.png)](src/RemoveOutermostParentheses.java) | |
279279
| 1022 | [Sum of Root to Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers) | | |
280280
| 1025 | [Divisor Game](https://leetcode.com/problems/divisor-game) | | |
281281
| 1029 | [Two City Scheduling](https://leetcode.com/problems/two-city-scheduling) | | |

src/RemoveOutermostParentheses.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class RemoveOutermostParentheses {
2+
public String removeOuterParentheses(String s) {
3+
StringBuilder result = new StringBuilder();
4+
for (int index = 0, level = 0 ; index < s.length() ; index++) {
5+
if (level != 0 && (level != -1 || s.charAt(index) != ')')) {
6+
result.append(s.charAt(index));
7+
}
8+
if (s.charAt(index) == '(') level--;
9+
else level++;
10+
}
11+
return result.toString();
12+
}
13+
}

0 commit comments

Comments
 (0)