Skip to content

Commit af850e3

Browse files
committed
Solutions added for LC problem # 678
1 parent 03373ea commit af850e3

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.sean.array;
2+
3+
import java.util.Arrays;
4+
import java.util.Stack;
5+
6+
/***
7+
* 678. Valid Parenthesis String
8+
*/
9+
public class ParenthesisValidator {
10+
// region Counting O(N)
11+
public boolean checkValidString(String s) {
12+
int minOp = 0;
13+
int maxOp = 0;
14+
15+
for (char c : s.toCharArray()) {
16+
if (c == '(') ++minOp;
17+
else --minOp;
18+
19+
if (c != ')') ++maxOp;
20+
else --maxOp;
21+
22+
if (maxOp < 0)
23+
return false;
24+
25+
minOp = Math.max(0, minOp);
26+
}
27+
return minOp == 0;
28+
}
29+
// endregion
30+
31+
// region DP (Top-Down) O(N^3)
32+
public boolean checkValidString1(String s) {
33+
// L[ XXX ]R
34+
// [LxR LxR]
35+
int len = s.length();
36+
caches = new int[len][len];
37+
for (int[] cacheResult : caches) {
38+
Arrays.fill(cacheResult, -1);
39+
}
40+
41+
return checkValidStringHelper(s, 0, len - 1);
42+
}
43+
44+
private int[][] caches;
45+
46+
private boolean checkValidStringHelper(String s, int left, int right) {
47+
if (left > right) {// empty string ?!
48+
caches[left][right] = 1;
49+
return true;
50+
}
51+
52+
if (caches[left][right] >= 0)
53+
return caches[left][right] == 1;
54+
55+
char leftCh = s.charAt(left);
56+
char rightCh = s.charAt(right);
57+
if (left == right) {
58+
caches[left][right] = leftCh == '*' ? 1 : 0;
59+
return caches[left][right] == 1;
60+
}
61+
62+
// // x[xxx]x
63+
if ((leftCh == '(' || leftCh == '*')
64+
&& (rightCh == ')' || rightCh == '*')
65+
&& checkValidStringHelper(s, left + 1, right - 1)) {
66+
67+
caches[left][right] = 1;
68+
return true;
69+
}
70+
71+
for (int i = left; i < right; i++) {
72+
if (checkValidStringHelper(s, left, i) && checkValidStringHelper(s, i + 1, right)) {
73+
caches[left][right] = 1;
74+
return true;
75+
}
76+
}
77+
78+
caches[left][right] = 0;
79+
return false;
80+
}
81+
// endregion
82+
83+
// region Double Stacks O(N)
84+
public boolean checkValidString0(String s) {
85+
Stack<Integer> stackLeft = new Stack<>();
86+
Stack<Integer> stackStar = new Stack<>();
87+
for (int i = 0; i < s.length(); i++) {
88+
char c = s.charAt(i);
89+
if (c == '(') {
90+
stackLeft.push(i);
91+
} else if (c == '*') {
92+
stackStar.push(i);
93+
} else {
94+
// balance ')'
95+
if (!stackLeft.empty()) {
96+
stackLeft.pop();
97+
} else if (!stackStar.empty()) {
98+
stackStar.pop();
99+
} else {
100+
return false;
101+
}
102+
}
103+
104+
}
105+
while (!stackLeft.empty() && !stackStar.empty()) {
106+
if (stackLeft.pop() > stackStar.pop()) { // any '*' before '(' ?
107+
return false;
108+
}
109+
}
110+
return stackLeft.empty();
111+
}
112+
// endregion
113+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class ParenthesisValidatorTest {
9+
10+
private ParenthesisValidator validator;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
validator = new ParenthesisValidator();
15+
}
16+
17+
@Test
18+
public void checkValidString() {
19+
assertTrue(validator.checkValidString("(*))"));
20+
}
21+
}

0 commit comments

Comments
 (0)