Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Valid Parantheses solution to python folder #143

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions python/valid_parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Problem statement:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.

"""
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# create a stack to the brackets
stacks = []

# define the brackets and their corresponding pairs
brackets = {')': '(', '}': '{', ']': '['}

# iterate over each character in the string
for char in s:
# if the character is an open bracket, push it to the stack
if char in brackets.values():
stacks.append(char)
# if the character is a close bracket
else:
# if the stack is empty or the top of the stack does not match the corresponding open bracket, return False
if not stacks or brackets[char] != stacks.pop():
return False

# if the stack is empty, the string is valid
return not stacks


# Example usage:
"""
Input: s = "()"
Output: true

Input: s = "()[]{}"
Output: true

Input: s = "(]"
Output: false

Input: s = "([)]"
Output: false
"""
Loading