Skip to content

Instantly share code, notes, and snippets.

@SahilKadam
Created October 14, 2016 23:55
Show Gist options
  • Save SahilKadam/0346bcc92a02a61d17ec6c733a5a395f to your computer and use it in GitHub Desktop.
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.
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)
@asadali08
Copy link

asadali08 commented Jul 16, 2019

Extremely helpful. Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment