Skip to content

Commit df4511d

Browse files
add 1190
1 parent 69ffa7c commit df4511d

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ _If you like this project, please leave me a star._ ★
6666
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
6767
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo)|Easy||
6868
|1196|[How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1196.java) | [:tv:](https://www.youtube.com/watch?v=UelshlMQNJM)|Easy||
69+
|1190|[Reverse Substrings Between Each Pair of Parentheses](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1190.java) | |Medium|Stack|
6970
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1189.java) |[:tv:](https://youtu.be/LGgMZC0vj5s) |Easy||
7071
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | |Easy||
7172
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1184.java) | [:tv:](https://www.youtube.com/watch?v=RFq7yA5iyhI)|Easy||
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
import java.util.Stack;
6+
7+
/**
8+
* 1190. Reverse Substrings Between Each Pair of Parentheses
9+
*
10+
* You are given a string s that consists of lower case English letters and brackets.
11+
* Reverse the strings in each pair of matching parentheses, starting from the innermost one.
12+
* Your result should not contain any brackets.
13+
*
14+
* Example 1:
15+
* Input: s = "(abcd)"
16+
* Output: "dcba"
17+
*
18+
* Example 2:
19+
* Input: s = "(u(love)i)"
20+
* Output: "iloveu"
21+
* Explanation: The substring "love" is reversed first, then the whole string is reversed.
22+
*
23+
* Example 3:
24+
* Input: s = "(ed(et(oc))el)"
25+
* Output: "leetcode"
26+
* Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
27+
*
28+
* Example 4:
29+
* Input: s = "a(bcdefghijkl(mno)p)q"
30+
* Output: "apmnolkjihgfedcbq"
31+
*
32+
* Constraints:
33+
* 0 <= s.length <= 2000
34+
* s only contains lower case English characters and parentheses.
35+
* It's guaranteed that all parentheses are balanced.
36+
* */
37+
public class _1190 {
38+
public static class Solution1 {
39+
public String reverseParentheses(String s) {
40+
Stack<Character> stack = new Stack<>();
41+
Queue<Character> queue = new LinkedList<>();
42+
for (char c : s.toCharArray()) {
43+
if (c != ')') {
44+
stack.push(c);
45+
} else {
46+
while (!stack.isEmpty() && stack.peek() != '(') {
47+
queue.offer(stack.pop());
48+
}
49+
if (!stack.isEmpty()) {
50+
stack.pop();//pop off the open paren
51+
}
52+
while (!queue.isEmpty()) {
53+
stack.push(queue.poll());
54+
}
55+
}
56+
}
57+
StringBuilder sb = new StringBuilder();
58+
while (!stack.isEmpty()) {
59+
sb.append(stack.pop());
60+
}
61+
return sb.reverse().toString();
62+
}
63+
}
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1190;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1190Test {
10+
private static _1190.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1190.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("dcba", solution1.reverseParentheses("(abcd)"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("iloveu", solution1.reverseParentheses("(u(love)i)"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("leetcode", solution1.reverseParentheses("(ed(et(oc))el)"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("apmnolkjihgfedcbq", solution1.reverseParentheses("a(bcdefghijkl(mno)p)q"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)