Menu
CoddyTech

Valid Parentheses

EasyStackpython iconjava iconcpp iconc iconjs icon+10

A string of brackets is balanced when every opening bracket is closed by a bracket of the same kind, and the pairs sit inside each other instead of overlapping. There are three kinds: round (), square [] and curly {}.

For example, {[()()]} is balanced: each pair closes inside the pair that wraps it. But {(}) is not: the curly bracket closes while the round bracket opened after it is still waiting. A string like (( is not balanced either, because nothing closes the two openers.

Write a function named isValid that gets a string s made only of the characters (, ), [, ], { and }, and returns true when its brackets are balanced and false otherwise.

Balanced means every closing bracket matches the most recent opening bracket that is still open, the two are the same kind, and no opening bracket is left open at the end.

Constraints: 1 ≤ s.length ≤ 10^4.

Function

isValid(arg1: string) → boolean
arg1string
Returnsboolean

Examples

Input
arg1 = "[]{}()"
Output
true

lock icon+13 hidden tests on Submit

Reset code
def isValid(s):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

arg1 = "[]{}()"

Expected

true