Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 24, 2022 02:47 pm GMT

Day 2 - Valid Parentheses

-> Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order.

Eg ->

Input: s = "()"
Output: true

class Solution:    def isValid(self, s: str) -> bool:        stack =[]        for i in s:             if i == "(" or i == "{" or i == "[" :                 stack.append(i)            elif i == ")" :                 if stack and stack[-1]=="(":                      stack.pop()                else:                     stack.append(i)            elif i == "}" :                 if stack and stack[-1]=="{":                     stack.pop()                else:                     stack.append(i)            elif i == "]" :                 if stack and stack[-1]=="[":                     stack.pop()                else:                     stack.append(i)        if stack:            return False         else:             return True

Original Link: https://dev.to/ikokkili/day-2-valid-parentheses-3elc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To