Created
October 14, 2016 23:55
-
-
Save SahilKadam/0346bcc92a02a61d17ec6c733a5a395f to your computer and use it in GitHub Desktop.
For this question, you will parse a string to determine if it contains only "balanced delimiters." A balanced delimiter starts with an opening character ((, [, {), ends with a matching closing character (), ], } respectively), and has only other matching delimiters in between. A balanced delimiter may contain any number of balanced delimiters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = input().strip() | |
lst = [] | |
for i in range(len(array)): | |
if array[i] == '(' or array[i] == '[' or array[i] == '{': | |
lst.append(array[i]) | |
elif array[i] == ')': | |
if len(lst) != 0 and lst[len(lst)-1] == '(': | |
lst.pop() | |
else: | |
lst.append(array[i]) | |
elif array[i] == ']': | |
if len(lst) != 0 and lst[len(lst)-1] == '[': | |
lst.pop() | |
else: | |
lst.append(array[i]) | |
elif array[i] == '}': | |
if len(lst) != 0 and lst[len(lst)-1] == '{': | |
lst.pop() | |
else: | |
lst.append(array[i]) | |
print (len(lst) == 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extremely helpful. Thank you so much