Skip to content

Commit

Permalink
Merge pull request #41 from akashsara/issue36-valid-parenthesis
Browse files Browse the repository at this point in the history
Added solution to issue 36 - Valid Parenthesis
  • Loading branch information
the-vampiire authored Oct 7, 2018
2 parents 7e27f32 + 626ce91 commit 8ef545b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions intermediate/valid_parenthesis_akashsara.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!python3

def has_valid_parens(input_string):
"""
CHALLENGE:
Given a string 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.
> Note that an empty string is also considered valid.
DIFFICULTY: INTERMEDIATE [Due to use of stack]
"""
stack = []
bracket_map = {'[':']', '(':')', '{':'}'}
for bracket in input_string:
if bracket in bracket_map:
stack.append(bracket)
elif len(stack) == 0 or bracket_map[stack.pop()] != bracket:
return False
return len(stack) == 0

0 comments on commit 8ef545b

Please sign in to comment.