-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0678-valid-parenthesis-string.py
More file actions
110 lines (95 loc) · 3.1 KB
/
0678-valid-parenthesis-string.py
File metadata and controls
110 lines (95 loc) · 3.1 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
107
108
109
110
# time complexity: O(n)
# space complexity: O(n)
from functools import lru_cache
class Solution:
def checkValidString(self, s: str) -> bool:
stack = []
starStack = []
for i, ch in enumerate(s):
if ch == "(":
stack.append(i)
elif ch == "*":
starStack.append(i)
else:
if stack:
stack.pop()
elif starStack:
starStack.pop()
else:
return False
while stack and starStack:
if stack.pop() > starStack.pop():
return False
return not stack
# time complexity: O(n^2)
# space complexity: O(n^2)
class Solution:
def checkValidString(self, s: str) -> bool:
n = len(s)
@lru_cache(None)
def dp(idx: int, openCount: int) -> bool:
if idx == n:
return openCount == 0
if openCount < 0:
return False
if s[idx] == '*':
return (
dp(idx + 1, openCount + 1) or
(openCount > 0 and dp(idx + 1, openCount - 1)) or
dp(idx + 1, openCount)
)
elif s[idx] == '(':
return dp(idx + 1, openCount + 1)
else:
return openCount > 0 and dp(idx + 1, openCount - 1)
return dp(0, 0)
# time complexity: O(n^2)
# space complexity: O(n^2)
class Solution:
def checkValidString(self, s: str) -> bool:
n = len(s)
dp = [[False for _ in range(n + 1)] for _ in range(n + 1)]
dp[n][0] = True
for i in range(n - 1, -1, -1):
for openCount in range(n):
if s[i] == '(':
if openCount <= n - 1:
dp[i][openCount] = dp[i + 1][openCount + 1]
elif s[i] == ')':
if openCount > 0:
dp[i][openCount] = dp[i + 1][openCount - 1]
else:
result = False
if openCount <= n - 1:
result |= dp[i + 1][openCount + 1]
if openCount > 0:
result |= dp[i + 1][openCount - 1]
result |= dp[i + 1][openCount]
dp[i][openCount] = result
return dp[0][0]
# time complexity: O(n)
# space complexity: O(1)
class Solution:
def checkValidString(self, s: str) -> bool:
openCount = 0
closeCount = 0
n = len(s)
for left in range(n):
right = n - 1 - left
if s[left] == '(' or s[left] == '*':
openCount += 1
else:
openCount -= 1
if s[right] == ')' or s[right] == '*':
closeCount += 1
else:
closeCount -= 1
if openCount < 0 or closeCount < 0:
return False
return True
s = "()"
print(Solution().checkValidString(s))
s = "(*)"
print(Solution().checkValidString(s))
s = "(*))"
print(Solution().checkValidString(s))