Skip to content

Commit 6686627

Browse files
authored
Add python code for 32 Longest Valid Parentheses & Update README.md (#150)
1 parent 524a94f commit 6686627

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Python/longest-valid-parentheses.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Speed: 95.97%
3+
Memory: 24.96%
4+
Time complexity: O(n)
5+
Space complexity: O(n)
6+
'''
7+
class Solution(object):
8+
def longestValidParentheses(self, s):
9+
ans=0
10+
stack=[-1]
11+
for i in range(len(s)):
12+
if(s[i]=='('):
13+
stack.append(i)
14+
else:
15+
stack.pop()
16+
if(len(stack)==0):
17+
stack.append(i)
18+
else:
19+
ans=max(ans,i-stack[-1])
20+
return ans

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
202202
| 735 | [Asteroid Collision](https://leetcode.com/problems/asteroid-collision/) | [C++](./C++/asteroid-collision.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
203203
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [C++](./C++/decode-string.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
204204
| 921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [C++](./C++/minimum-add-to-make-parentheses-valid.cpp) | _O(n)_ | _O(1)_ | Medium | Stack | |
205-
205+
| 32 | [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) | [Python](.Python/longest-valid-parentheses.py) | _O(n)_ | _O(n)_ | Hard | Stack | |
206206
<br/>
207207
<div align="right">
208208
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -475,6 +475,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
475475
| [James H](https://github.com/HoangJames) <br> <img src="https://avatars2.githubusercontent.com/u/15057250?s=460&v=4" width="100" height="100"> | United Kingdom | C++ | [Github](https://github.com/HoangJames) |
476476
| [Franchis N. Saikia](https://github.com/Francode007) <br> <img src="https://avatars0.githubusercontent.com/u/63102937?s=400&v=4" width="100" height="100"> | India | C++ | [Github](https://github.com/Francode007) |
477477
| [Yarncha](https://github.com/yarncha) <br> <img src="https://avatars0.githubusercontent.com/u/33623182?s=400&v=4" width="100" height="100"> | South Korea | C++ | [LeetCode](https://leetcode.com/yamcha/) |
478+
| [Gamez0](https://github.com/Gamez0) <br> <img src="https://avatars3.githubusercontent.com/u/34051876?s=400&v=4" width="100" height="100"> | South Korea | Python | [LeetCode](https://leetcode.com/Gamez0/) |
478479
<br/>
479480
<div align="right">
480481
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)