An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
May 24, 2022 02:47 pm GMT
Original Link: https://dev.to/ikokkili/day-2-valid-parentheses-3elc
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To