- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathValidParenthesisString678.java
More file actions
Latest commit
106 lines (97 loc) · 3.33 KB
/
Copy pathValidParenthesisString678.java
File metadata and controls
106 lines (97 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Given a string containing only three types of characters: '(', ')' and '*',
* write a function to check whether this string is valid. We define the
* validity of a string by these rules:
*
* Any left parenthesis '(' must have a corresponding right parenthesis ')'.
* Any right parenthesis ')' must have a corresponding left parenthesis '('.
* Left parenthesis '(' must go before the corresponding right parenthesis ')'.
* '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
* An empty string is also valid.
*
* Example 1:
* Input: "()"
* Output: True
*
* Example 2:
* Input: "(*)"
* Output: True
*
* Example 3:
* Input: "(*))"
* Output: True
*
* Note:
* The string size will be in the range [1, 100].
*/
publicclassValidParenthesisString678 {
publicbooleancheckValidString(Strings) {
returncheckValidString(s.toCharArray(), 0, newint[]{0});
}
publicbooleancheckValidString(char[] chars, inti, int[] count) {
if (i == chars.length) returncount[0] == 0;
charc = chars[i];
if (c == '(') {
count[0]++;
returncheckValidString(chars, i + 1, count);
} elseif (c == ')') {
if (count[0] == 0) returnfalse;
count[0]--;
returncheckValidString(chars, i + 1, count);
} else {
intpre = count[0];
if (checkValidString(chars, i + 1, count)) returntrue;
count[0] = pre + 1;
if (checkValidString(chars, i + 1, count)) returntrue;
count[0] = pre;
if (count[0] == 0) returnfalse;
count[0]--;
returncheckValidString(chars, i + 1, count);
}
}
/**
* https://leetcode.com/problems/valid-parenthesis-string/solution/
*/
publicbooleancheckValidString2(Strings) {
intn = s.length();
if (n == 0) returntrue;
boolean[][] dp = newboolean[n][n];
for (inti = 0; i < n; i++) {
if (s.charAt(i) == '*') dp[i][i] = true;
if (i < n-1 &&
(s.charAt(i) == '(' || s.charAt(i) == '*') &&
(s.charAt(i+1) == ')' || s.charAt(i+1) == '*')) {
dp[i][i+1] = true;
}
}
for (intsize = 2; size < n; size++) {
for (inti = 0; i + size < n; i++) {
if (s.charAt(i) == '*' && dp[i+1][i+size] == true) {
dp[i][i+size] = true;
} elseif (s.charAt(i) == '(' || s.charAt(i) == '*') {
for (intk = i+1; k <= i+size; k++) {
if ((s.charAt(k) == ')' || s.charAt(k) == '*') &&
(k == i+1 || dp[i+1][k-1]) &&
(k == i+size || dp[k+1][i+size])) {
dp[i][i+size] = true;
}
}
}
}
}
returndp[0][n-1];
}
/**
* https://leetcode.com/problems/valid-parenthesis-string/solution/
*/
publicbooleancheckValidString3(Strings) {
intlo = 0, hi = 0;
for (charc: s.toCharArray()) {
lo += c == '(' ? 1 : -1;
hi += c != ')' ? 1 : -1;
if (hi < 0) break;
lo = Math.max(lo, 0);
}
returnlo == 0;
}
}