Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
Simpler way in Python

Mohammed Dh Abbas

Jul 22, 2024

class Solution: def isValid(self, s): stack = [] for char in s: if char == '{' or char == '[' or char == '(': stack.append(char) else: popped = stack.pop() if popped == '{' and char != '}': return False elif popped == '[' and char != ']': return False elif popped == '(' and char != ')': return False return len(stack) == 0

0

0

Comments
Comments
Ayush Bhandari
Ayush Bhandari10 months ago

if the first element is a closing parenthesis the code will fail.

eg: '}[]'

On this page