20. Valid Parentheses

Leetcode #: 20
Name: Valid Parentheses
Difficulty: Easy
Companies asked: Airbnb, Google, LinkedIn, Meta, Twitter
Link: https://leetcode.com/problems/valid-parentheses/

'(', ')', '{', '}', '[', ']'
stack = []
hashmap = {
")":"(",
"}":"{",
"]":"["
}
for char in s:
if char in hashmap:
# stack.pop() logic
else:
stack.append(char)
if stack and stack[-1] == hashmap[char]:
stack.pop()
else:
return False
return True if not stack else False
Python

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Kim

John Kim

75 Followers

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com