Skip to content

Commit ce1cfeb

Browse files
add 1021
1 parent a7c71ac commit ce1cfeb

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3031
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
3132
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
3233
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**1021. Remove Outermost Parentheses
7+
*
8+
* A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
9+
*
10+
* A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
11+
*
12+
* Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
13+
*
14+
* Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
15+
*
16+
*
17+
* Example 1:
18+
* Input: "(()())(())"
19+
* Output: "()()()"
20+
* Explanation:
21+
* The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
22+
* After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
23+
*
24+
* Example 2:
25+
* Input: "(()())(())(()(()))"
26+
* Output: "()()()()(())"
27+
* Explanation:
28+
* The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
29+
* After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
30+
*
31+
* Example 3:
32+
* Input: "()()"
33+
* Output: ""
34+
* Explanation:
35+
* The input string is "()()", with primitive decomposition "()" + "()".
36+
* After removing outer parentheses of each part, this is "" + "" = "".
37+
*
38+
* Note:
39+
*
40+
* S.length <= 10000
41+
* S[i] is "(" or ")"
42+
* S is a valid parentheses string
43+
* */
44+
public class _1021 {
45+
public static class Solution1 {
46+
public String removeOuterParentheses(String S) {
47+
List<String> primitives = new ArrayList<>();
48+
for (int i = 1; i < S.length(); i++) {
49+
int initialI = i - 1;
50+
int left = 1;
51+
while (i < S.length() && left > 0) {
52+
if (S.charAt(i) == '(') {
53+
left++;
54+
} else {
55+
left--;
56+
}
57+
i++;
58+
}
59+
primitives.add(S.substring(initialI, i));
60+
}
61+
StringBuilder sb = new StringBuilder();
62+
for (String primitive : primitives) {
63+
sb.append(primitive.substring(1, primitive.length() - 1));
64+
}
65+
return sb.toString();
66+
}
67+
}
68+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1021;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1021Test {
10+
private static _1021.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1021.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("()()()", solution1.removeOuterParentheses("(()())(())"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("()()()()(())", solution1.removeOuterParentheses("(()())(())(()(()))"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("", solution1.removeOuterParentheses("()()"));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)