You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Leetcode_Solutions/Python/678._Valid_Parenthesis_String.md
+113-1Lines changed: 113 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,93 @@ The string size will be in the range [1, 100].
34
34
35
35
## 解题方案
36
36
37
+
37
38
> 思路 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
+
classSolution(object):
53
+
defcheckValidString(self, s):
54
+
"""
55
+
:type s: str
56
+
:rtype: bool
57
+
"""
58
+
ifnot s orlen(s) ==0:
59
+
returnTrue
60
+
61
+
LEFTY, RIGHTY='(*', ')*'
62
+
63
+
n =len(s)
64
+
dp = [[0] * n for _ in s]
65
+
for i inrange(n):
66
+
if s[i] =='*':
67
+
dp[i][i] =1
68
+
if i < n-1and s[i] inLEFTYand s[i+1] inRIGHTY:
69
+
dp[i][i+1] =1
70
+
for j inrange(n):
71
+
for i inrange(j-2, -1, -1):
72
+
if s[i] =='*'and dp[i+1][j]:
73
+
dp[i][j] =1
74
+
elif s[i] inLEFTY:
75
+
for k inrange(i+1, j+1):
76
+
if s[k] inRIGHTYand \
77
+
(k == i+1or dp[i+1][k-1]) and \
78
+
(k == j or dp[k+1][j]):
79
+
dp[i][j] =1
80
+
81
+
returnTrueif dp[0][-1] elseFalse
82
+
```
83
+
84
+
或者后面的循环按照子串的长度size来遍历,
85
+
86
+
```python
87
+
classSolution(object):
88
+
defcheckValidString(self, s):
89
+
"""
90
+
:type s: str
91
+
:rtype: bool
92
+
"""
93
+
ifnot s orlen(s) ==0:
94
+
returnTrue
95
+
96
+
LEFTY, RIGHTY='(*', ')*'
97
+
98
+
n =len(s)
99
+
dp = [[False] * n for _ in s]
100
+
for i inrange(n):
101
+
if s[i] =='*':
102
+
dp[i][i] =True
103
+
if i < n-1and s[i] inLEFTYand s[i+1] inRIGHTY:
104
+
dp[i][i+1] =True
105
+
106
+
for size inrange(2, n):
107
+
for i inrange(n - size):
108
+
if s[i] =='*'and dp[i+1][i+size]:
109
+
dp[i][i+size] =True
110
+
elif s[i] inLEFTY:
111
+
for k inrange(i+1, i+size+1):
112
+
if (s[k] inRIGHTYand \
113
+
(k == i+1or 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
+
```
39
119
120
+
> 思路 2
121
+
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
40
122
123
+
这是我的最初方法
41
124
42
125
- Keep track of indices for unmatched '(' and available '*'.
43
126
- After the loop we can check whether the index of any remaining '*' can match the unmatched '('.
@@ -81,10 +164,39 @@ class Solution(object):
81
164
returnnot stack
82
165
```
83
166
167
+
> 思路 3
168
+
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
84
169
85
170
171
+
看到别人的解法,真的🐂p,贪心算法
86
172
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
87
176
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.
0 commit comments