forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0678-valid-parenthesis-string.js
112 lines (88 loc) · 2.87 KB
/
0678-valid-parenthesis-string.js
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
107
108
109
110
/**
* @param {string} s
* @return {boolean}
*/
var checkValidString = function (s) {
var leftMin = 0;
var leftMax = 0;
for (var c of s) {
if (c === '(') {
leftMin++;
leftMax++;
} else if (c === ')') {
leftMin--;
leftMax--;
} else {
leftMin--;
leftMax++;
}
if (leftMax < 0) {
return false;
}
if (leftMin < 0) {
leftMin = 0;
}
}
return leftMin === 0;
};
/**
* https://leetcode.com/problems/valid-parenthesis-string/
* Time O(N^3) | Space O(N^2)
* @param {string} s
* @return {boolean}
*/
var checkValidString = function(s) {
const isBaseCase = s.length === 0;
if (isBaseCase) return true;
const dp = new Array(s.length).fill()
.map(() => new Array(s.length).fill(false));
for (let i = 0; i < s.length; i++) {/* Time O(N) */
if (isStar(s[i])) dp[i][i] = true;
const isInBound = i < (s.length - 1)
const isOpenedOrStar = isOpened(s[i]) || isStar(s[i])
const isClosedOrStar = isClosed(s[i + 1]) || isStar(s[i + 1])
const isValid = isInBound && isOpenedOrStar && isClosedOrStar
if (isValid) dp[i][i + 1] = true;/* Space O(N^2) */
}
for (let size = 2; size < s.length; size++) {/* Time O() */
for (let i = 0; i + size < s.length; i++) {/* Time O(N) */
const isStarOrDP = isStar(s[i]) && isDP(dp, (i + 1), (i + size))
if (isStarOrDP) { dp[i][i + size] = true; continue; }
const isOpenedOrStar = isOpened(s[i]) || isStar(s[i])
if (isOpenedOrStar) check(dp, size, i); /* Time O(N) */
}
}
return dp[0][s.length - 1];
}
const check = (dp, size, i) => {
for (let k = (i + 1); k <= (i + size); k++) {/* Time O(N) */
const isClosedOrStar = isClosed(s[k]) || isStar(s[k])
const isKOrDP = isKEqual(k, i, 1) || isDP(dp, (i + 1), (k - 1))
const isKOrDPSize = isKEqual(k, i, size) || isDP(dp, (k + 1), (i + size))
const isValid = isClosedOrStar && isKOrDP && isKOrDPSize
if (isValid) dp[i][i + size] = true;/* Space O(N^2) */
}
}
var isStar = (char) => char === '*'
var isOpened = (char) => char === '('
var isClosed = (char) => char === ')'
const isKEqual = (k, i, size) => k === (i + size)
const isDP = (dp, i, k) => dp[i][k]
/**
* Time O(N) | Space O(1)
* @param {string} s
* @return {boolean}
*/
var checkValidString = function(s) {
let [ left, right ] = [ 0, 0 ];
for (const char of s) {/* Time O(N) */
left += isOpened(char) ? 1 : -1;
right += !isClosed(char) ? 1 : -1;
const isNegative = right < 0;
if (isNegative) break;
left = Math.max(left, 0);
}
return left === 0;
}
var isOpened = (char) => char === '('
var isClosed = (char) => char === ')'