Leetcode 20. Valid Parentheses

sateesh.py
1 min readNov 12, 2023

--

class Solution:
def isValid(self, s: str) -> bool:
stack = []
for char in s:
if char in ['(','{','[']:
stack.append(char)
else:
if not stack:
return False
cp = stack.pop()
if cp == '(':
if char != ')':
return False
elif cp == '{':
if char != '}':
return False
if cp == '[':
if char != ']':
return False
if stack:
return False
return True

i sloved it using Stack and its operations like append and pop. i used list data structure in Python3

--

--

sateesh.py
sateesh.py

Written by sateesh.py

Python & data enthusiast | Helping businesses extract insights from their data | Experienced in Python, SQL, and data visualization

No responses yet