[LeetCode]#20. Valid Parentheses

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
Input: s = "()"
Output: true
Input: s = "()[]{}"
Output: true
Input: s = "(]"
Output: false
Input: s = "([)]"
Output: false
Input: s = "{[]}"
Output: true
  1. Create a mapping dictionary
  2. input your test example
class Solution(object):
def isValid(self, s):
stack = []
mapping = {")": "(", "}": "{", "]": "["}
for char in s:
if char in mapping:
top_element = stack.pop() if stack else '#'
if mapping[char] != top_element:
return False
else:
stack.append(char)
return not stack

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrate Amazon QuickSight with Jira

BrailleBox: Building a Braille news reader with Android Things

Software Design, Separation Of Concerns & Unit Testing

Vault 1.0.X in Kubernetes with auto unsealing

Apache Spark: 5 Performance Optimization Tips

Extracting running data out of NRC/Nike + (Nike Run Club) using API’s

AWS CloudFormation -Update Rollback Failed -How to Rectify in Production Environment

Differences between static and dynamic libraries

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
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2057. Smallest Index With Equal Value

Remove nth node from end of list Leetcode Python

How to implement a Queue class using deque. PYTHON data structures and algorithm.

Data Structures Cookbook