Skip to content

Commit 21942c3

Browse files
authored
Update 678._Valid_Parenthesis_String.md
1 parent 3271d84 commit 21942c3

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

docs/Leetcode_Solutions/Python/678._Valid_Parenthesis_String.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,93 @@ The string size will be in the range [1, 100].
3434

3535
## 解题方案
3636

37+
3738
> 思路 1
38-
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
39+
******- 时间复杂度: O(N^3)******- 空间复杂度: O(N^2)******
40+
41+
```
42+
Let dp[i][j] be true if and only if the interval s[i], s[i+1], ..., s[j] can be made valid. Then dp[i][j] is true only if:
43+
44+
s[i] is '*', and the interval s[i+1], s[i+2], ..., s[j] can be made valid;
45+
46+
or, s[i] can be made to be '(', and there is some k in [i+1, j] such that s[k] can be made to be ')', plus the two intervals cut by s[k] (s[i+1: k] and s[k+1: j+1]) can be made valid;
47+
```
48+
因为文章要从垃圾算法写到好的算法,这不是我的第一解法
49+
50+
但是是真的垃圾,才beats 2.75%
51+
```python
52+
class Solution(object):
53+
def checkValidString(self, s):
54+
"""
55+
:type s: str
56+
:rtype: bool
57+
"""
58+
if not s or len(s) == 0:
59+
return True
60+
61+
LEFTY, RIGHTY = '(*', ')*'
62+
63+
n = len(s)
64+
dp = [[0] * n for _ in s]
65+
for i in range(n):
66+
if s[i] == '*':
67+
dp[i][i] = 1
68+
if i < n-1 and s[i] in LEFTY and s[i+1] in RIGHTY:
69+
dp[i][i+1] = 1
70+
for j in range(n):
71+
for i in range(j-2, -1, -1):
72+
if s[i] == '*' and dp[i+1][j]:
73+
dp[i][j] = 1
74+
elif s[i] in LEFTY:
75+
for k in range(i+1, j+1):
76+
if s[k] in RIGHTY and \
77+
(k == i+1 or dp[i+1][k-1]) and \
78+
(k == j or dp[k+1][j]):
79+
dp[i][j] = 1
80+
81+
return True if dp[0][-1] else False
82+
```
83+
84+
或者后面的循环按照子串的长度size来遍历,
85+
86+
```python
87+
class Solution(object):
88+
def checkValidString(self, s):
89+
"""
90+
:type s: str
91+
:rtype: bool
92+
"""
93+
if not s or len(s) == 0:
94+
return True
95+
96+
LEFTY, RIGHTY = '(*', ')*'
97+
98+
n = len(s)
99+
dp = [[False] * n for _ in s]
100+
for i in range(n):
101+
if s[i] == '*':
102+
dp[i][i] = True
103+
if i < n-1 and s[i] in LEFTY and s[i+1] in RIGHTY:
104+
dp[i][i+1] = True
105+
106+
for size in range(2, n):
107+
for i in range(n - size):
108+
if s[i] == '*' and dp[i+1][i+size]:
109+
dp[i][i+size] = True
110+
elif s[i] in LEFTY:
111+
for k in range(i+1, i+size+1):
112+
if (s[k] in RIGHTY and \
113+
(k == i+1 or dp[i+1][k-1]) and \
114+
(k == i+size or dp[k+1][i+size])):
115+
dp[i][i+size] = True
116+
117+
return dp[0][-1]
118+
```
39119

120+
> 思路 2
121+
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
40122

123+
这是我的最初方法
41124

42125
- Keep track of indices for unmatched '(' and available '*'.
43126
- After the loop we can check whether the index of any remaining '*' can match the unmatched '('.
@@ -81,10 +164,39 @@ class Solution(object):
81164
return not stack
82165
```
83166

167+
> 思路 3
168+
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
84169

85170

171+
看到别人的解法,真的🐂p,贪心算法
86172

173+
```
174+
We know that at each point in the string, '( 'count should never be bigger than ')' count, and in the end the difference of two counts should be 0.
175+
So we have two boundaries: lowerBond and higherBound, which respectively represents the minimun possible difference and maximum possbile difference, as long as
87176
177+
higherBound is never below 0.
178+
in the end 0 is between lowerBond and higherBound
179+
in the string, when lowerbound is < 0, we make it back to 0.
180+
We know that the string is valid.
181+
```
88182

183+
beats 100%
89184

185+
```python
186+
class Solution(object):
187+
def checkValidString(self, s):
188+
"""
189+
:type s: str
190+
:rtype: bool
191+
"""
192+
lower_bound = higher_bound = 0
193+
for c in s:
194+
lower_bound += 1 if c == '(' else -1
195+
higher_bound += 1 if c != ')' else -1
196+
if higher_bound < 0:
197+
return False
198+
lower_bound = max(lower_bound, 0)
199+
200+
return lower_bound == 0
201+
```
90202

0 commit comments

Comments
 (0)