Skip to content

Commit f572910

Browse files
add a solution for 32
1 parent 43ccd93 commit f572910

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/main/java/com/fishercoder/solutions/_32.java

+46
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,50 @@ public int longestValidParentheses(String s) {
2323
return result;
2424
}
2525
}
26+
27+
public static class Solution2 {
28+
/**
29+
* my lengthy but original solution on 4/5/2021, the idea is to convert the valid parenthesis pairs into numbers and push them onto a stack.
30+
*/
31+
public int longestValidParentheses(String s) {
32+
Stack<String> stack = new Stack<>();
33+
int longest = 0;
34+
for (char c : s.toCharArray()) {
35+
if (c == '(') {
36+
stack.push(c + "");
37+
} else {
38+
if (stack.isEmpty()) {
39+
continue;
40+
} else {
41+
if (stack.peek().equals("(")) {
42+
stack.pop();
43+
stack.push("2");
44+
} else {
45+
int sum = 0;
46+
while (!stack.isEmpty() && !stack.peek().equals("(")) {
47+
sum += Integer.parseInt(stack.pop());
48+
}
49+
if (!stack.isEmpty()) {
50+
stack.pop();//pop off the open paren
51+
sum += 2;
52+
stack.push("" + sum);
53+
}
54+
longest = Math.max(longest, sum);
55+
}
56+
}
57+
}
58+
}
59+
int result = 0;
60+
while (!stack.isEmpty()) {
61+
if (stack.peek().equals("(")) {
62+
stack.pop();
63+
result = 0;
64+
} else {
65+
result += Integer.parseInt(stack.pop());
66+
longest = Math.max(result, longest);
67+
}
68+
}
69+
return longest;
70+
}
71+
}
2672
}

src/test/java/com/fishercoder/_32Test.java

+46
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,60 @@
88

99
public class _32Test {
1010
private static _32.Solution1 solution1;
11+
private static _32.Solution2 solution2;
1112

1213
@BeforeClass
1314
public static void setup() {
1415
solution1 = new _32.Solution1();
16+
solution2 = new _32.Solution2();
1517
}
1618

1719
@Test
1820
public void test1() {
1921
assertEquals(2, solution1.longestValidParentheses("(()"));
22+
assertEquals(2, solution2.longestValidParentheses("(()"));
2023
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(2, solution1.longestValidParentheses("()(()"));
28+
assertEquals(2, solution2.longestValidParentheses("()(()"));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(4, solution1.longestValidParentheses("(())("));
34+
assertEquals(4, solution2.longestValidParentheses("(())("));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals(6, solution1.longestValidParentheses("()(())"));
40+
assertEquals(6, solution2.longestValidParentheses("()(())"));
41+
}
42+
43+
@Test
44+
public void test5() {
45+
assertEquals(4, solution1.longestValidParentheses(")()())"));
46+
assertEquals(4, solution2.longestValidParentheses(")()())"));
47+
}
48+
49+
@Test
50+
public void test6() {
51+
assertEquals(4, solution1.longestValidParentheses(")()())()()("));
52+
assertEquals(4, solution2.longestValidParentheses(")()())()()("));
53+
}
54+
55+
@Test
56+
public void test7() {
57+
assertEquals(8, solution1.longestValidParentheses("((())())"));
58+
assertEquals(8, solution2.longestValidParentheses("((())())"));
59+
}
60+
61+
@Test
62+
public void test8() {
63+
assertEquals(10, solution1.longestValidParentheses(")()(((())))("));
64+
assertEquals(10, solution2.longestValidParentheses(")()(((())))("));
65+
}
66+
2167
}

0 commit comments

Comments
 (0)