All problems
Valid Parentheses
Problem
Determine whether the string s made of bracket characters is balanced.
Use a stack: push every opening bracket, and on a closing bracket verify it matches the most recent unmatched opener. Return True for the empty string and False on any mismatch or leftover opener.
Hint
A closing bracket must match the most recently opened one, which is exactly a last-in-first-out check.
Test cases
case 1
in: ["()"]
expected: true
case 2
in: ["()[]{}"]
expected: true
case 3
in: ["(]"]
expected: false
case 4
in: ["([{}])"]
expected: true
case 5
in: [""]
expected: true
Starter codePython
def is_valid_parentheses(s):
# Your code here
pass5 test cases · run your code against them in the editor