Problem:
The Balance of the World    The world should be finely balanced.  Positive vs. negative,
light vs. shadow, and left vs. right brackets.
Your mission is to write a program that judges whether a string is balanced
with respect to brackets so that we can observe the balance of the
world.    A string that will be given to the program may have two kinds of brackets,
round (“( )”) and square (“[ ]”).
A string is balanced if and only if the following conditions hold.  For every left round bracket (“(”), there is a corresponding
    right round bracket (“)”) in the following part of the string.  For every left square bracket (“[”), there is a corresponding
    right square bracket (“]”) in the following part of the string.  For every right bracket, there is a left bracket corresponding to it.  Correspondences of brackets have to be one to one, that is, a
    single bracket never corresponds to two or more brackets.  For every pair of corresponding left and right brackets,
    the substring between them is balanced.     Input   The input consists of one or more lines, each of which being a dataset.
A dataset is a string that consists of English alphabets,
space characters, and two kinds of brackets, round (“( )”) and square (“[ ]”),
terminated by a period.  You can assume that every line has 100
characters or less.
The line formed by a single period indicates the end of the input,
which is not a dataset.   Output   For each dataset, output “yes” if the
string is balanced, or “no” otherwise, in a line.
There may not be any extra characters in the output.   Sample Input  So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
 .
.  Output for the Sample Input  yes
yes
no
no
no
yes
yes

Solution (Python):
def parse(s, left, target_stack):
  if s[left] == "[":
    target_stack.append("]")
  if s[left] == "(":
    target_stack.append(")")
  
  if s[left] == "]":
    if target_stack and target_stack[-1] == "]":
      target_stack.pop()
    else:
      return False

  if s[left] == ")":
    if target_stack and target_stack[-1] == ")":
      target_stack.pop()
    else:
      return False

  left += 1
  if left == len(s):
    if target_stack:
      return False
    else:
      return True

  else:
    return parse(s, left, target_stack)

while True:
  s = input()
  if s == ".":
    break

  if parse(s, 0, []):
    print("yes")
  else:
    print("no")

Solution (Python):
ans_list = []
while True:
    s = input()
    if s == ".":
        break
    q_list = ["" for i in range(len(s))]
    now = -1
    ans = "yes"
    for i in list(s):
        if i == "(" or i == "[":
            now += 1
            q_list[now] = i
        elif i == ")":
            if now == -1:
                ans = "no"
            elif q_list[now] == "(":
                now -= 1
            else:
                ans = "no"
        elif i == "]":
            if now == -1:
                ans = "no"
            elif q_list[now] == "[":
                now -= 1
            else:
                ans = "no"
        if ans == "no":
            break
    if now >= 0:
        ans = "no"
    ans_list.append(ans)

for i in ans_list:
    print(i)

Solution (Python):
while True:
    s = input()
    if s == '.': break
    pl = [c for c in s if c == '(' or c == ')' or c == '[' or c == ']']
    flag = False
    while True:
        if len(pl) == 0:
            flag = True
            break
        if len(pl) == 1: break
        flag2 = False
        for i in range(len(pl) - 1):
            if (pl[i] == '(' and pl[i + 1] == ')') or (pl[i] == '[' and pl[i + 1] == ']'):
                del pl[i + 1]
                del pl[i]
                break
            elif i == len(pl) - 2:
                flag2 = True
        if flag2: break
    print('yes' if flag else 'no')

Solution (Python):
def check():
    while True:
        l=len(p)
        if l==0 or l==1: return l-1
        for i in range(l-1):
            if p[i]+p[i+1]=='()' or p[i]+p[i+1]=='[]':
                del p[i+1],p[i]
                break
        if l==len(p):return False
while True:
    s=input()
    if s=='.':break
    p = [c for c in s if c=='(' or c ==')' or c=='['or c==']']
    print('yes' if check() else 'no')

Solution (Python):

while(True):
	stack1=[]
	flag=-1
	st=raw_input()
	


	if st==".":
		break
	for s in st:
		if s=="(":
			stack1.append(s)
		elif s=="[":
			stack1.append(s)
		elif s==")":
			if len(stack1)==0:
				flag=0
				break
			if stack1[len(stack1)-1]!="(":
				flag=0
				break

			stack1.pop()
		elif s=="]":
			if len(stack1)==0:
				flag=0
				break
			if stack1[len(stack1)-1]!="[":
				flag=0
				break
			stack1.pop()


	if flag!=0 and len(stack1)==0:
		print "yes"
		continue
	print "no"
	continue

Solution (Python):



import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            s = raw_input()
            if s == '.':
                break
            for i in range(len(s)):
                escape = False
                if s[i] == ')':
                    for j in reversed(range(i)):
                        if s[j] == '(':
                            s = list(s)
                            s[i] = '*'
                            s[j] = '*'
                            s = ''.join(s)
                            break
                        elif s[j] == '[':
                            escape = True
                    else:
                        escape = True
                elif s[i] == ']':
                    for j in reversed(range(i)):
                        if s[j] == '[':
                            s = list(s)
                            s[i] = '*'
                            s[j] = '*'
                            s = ''.join(s)
                            break
                        elif s[j] == '(':
                            escape = True
                    else:
                        escape = True
                if escape:
                    print 'no'
                    break
                
            else:
                if '(' in s or '[' in s:
                    print 'no'
                else:
                    print 'yes'
                                      
        
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Solution (Python):
S = {']':'[',')':'('}
while 1:
    l = input()
    if l == '.':break

    lst = []
    flg = True
    for w in list(l):
        if w == '[' or w == '(':
            lst.append(w)
        if w== ']' or w == ')':
            if len(lst) > 0 and S[w] == lst[-1]:
                lst.pop(-1)
            else:
                flg = False
                break
    print('yes' if flg and len(lst) == 0 else 'no')

Solution (Python):
def check():
    while True:
        l=len(p)
        if l==0 or l==1: return l-1
        for i in range(l-1):
            if p[i]+p[i+1]=='()' or p[i]+p[i+1]=='[]':
                del p[i+1],p[i]
                break
        if l==len(p):return False
while True:
    s=input()
    if s=='.':break
    p = [c for c in s if c=='(' or c ==')' or c=='['or c==']']
    print('yes' if check() else 'no')

Solution (Python):


import sys
sys.setrecursionlimit(10**6)

def dfs(string,bracket):
    i = 0
    while i < len(string):
        if bracket == '[' and string[i] == ']':
            return i
        elif bracket == '(' and string[i] == ')':
            return i
        elif string[i] == ')' or string[i] == ']':
            return False
        elif string[i] == '(' or string[i] == '[':
            ret = dfs(string[i+1:],string[i])
            if ret is False:
                return False
            i += ret + 1
        i += 1
    return True if bracket == '' else False


while True:
    string = list(input())
    if len(string) == 1 and string[0] == '.':
        break
    ans = 'yes' if dfs(string,'') else 'no'
    print(ans)

Solution (Python):
while 1:
	sentence = raw_input()
	if sentence == ".":
		break
	bracket = []
	length = 0
	for s in sentence:
		if s == "(":
			bracket.append(s)
			length += 1
		elif s == ")":
			if len(bracket) == 0 or bracket.pop() != "(":
				length = -1
				break
			length -= 1
		elif s == "[":
			bracket.append(s)
			length += 1
		elif s == "]":
			if len(bracket) == 0 or bracket.pop() != "[":
				length = -1
				break
			length -= 1
	print "yes" if length == 0 else "no"

Solution (Python):



s = raw_input()
while s != '.':
    b = []
    for i in range(len(s)+1):
        if i == len(s):
            if len(b) == 0:
                print "yes"
                break
            else:
                print "no"
                break

        if s[i]=='(' or s[i]=='[':
            b.append(s[i])
        if s[i]==')' or s[i]==']':
            if len(b) == 0:
                print "no"
                break
            elif b[-1]=='(' and s[i]==')':
                b.pop()
            elif b[-1]=='[' and s[i]==']':
                b.pop()
            else:
                print "no"
                break

    s = raw_input()

Solution (Python):
while True:
    s = input()
    if s[0] == '.': break
    r = []
    f = False
    for c in s:
        if c == '(' or c == '[':
            r.append(c)
        elif c == ')':
            if not r or r.pop() != '(':
                print("no")
                break
        elif c == ']':
            if not r or r.pop() != '[':
                print("no")
                break
    else:
        print("yes" if not r else "no")

Solution (Python):
while True:
    flg = True
    S = input()
    if S == '.':
        exit()
    brackets = []
    for c in S:
        if c == '[':
            brackets.append('[')
        elif c == ']':
            if len(brackets) == 0:
                flg = False
                break
            if brackets.pop() != '[':
                flg = False
                break
        elif c == '(':
            brackets.append('(')
        elif c == ')':
            if len(brackets) == 0:
                flg = False
                break
            if brackets.pop() != '(':
                flg = False
                break
        else:
            pass
    if len(brackets) > 0:
        flg = False
    if flg:
        print('yes')
    else:
        print('no')

Solution (Python):


def solve(line):
    stack = []

    for i in range(len(line)):
        if line[i] == '(' or line[i] == '[':
            stack.append(line[i])
        elif len(stack) > 0:
            if line[i] == ')' and stack[-1] == '(':
                stack.pop()
            elif line[i] == ']' and stack[-1] == '[':
                stack.pop()
            elif line[i] == ']' or line[i] == ')':
                return 'no'
        elif line[i] == ']' or line[i] == ')':
            return 'no'

    if len(stack) == 0:
        return 'yes'
    else:
        return 'no'




if __name__ == '__main__':
    while True:
        line = input()

        if line == ".":
            break

        result = solve(line)
        print(result)

Solution (Python):

while True:
	s = input()
	if s == '.':
		break
	flag = False
	stack = []
	for c in s:
		if c in '[(':
			stack.append(c)
		elif c in '])':
			if c == ']' and len(stack)>0 and stack[-1] == '[':
				stack.pop()
			elif c == ')' and len(stack)>0 and stack[-1] == '(':
				stack.pop()
			else:
				flag = True
				break
	if stack or flag:
		print('no')
	else:
		print('yes')

Solution (Python):
while True:
    s = input()
    if s == ".":
        break
    s = "".join([c for c in s if c in "()[]"])
    while "()" in s or "[]" in s:
        s = s.replace("()", "")
        s = s.replace("[]", "")
    print("no" if s else "yes")

Solution (Python):
import re
def isSet(r,l):
    while True:
        if not(l): return
        if l[0]==r:
            del l[0]
            return l
        elif l[0]=='(' or l[0]=='[':
            x = ')' if l[0]=='(' else ']'
            del l[0]
            l = isSet(x,l)
        else: return None
t = []
while True:
    s = input()
    if s=='.': break
    l = list(re.sub(r'[a-zA-Z. ]+','',s))
    while True:
        if not(l): break
        if l[0]==')' or l[0]==']':
            l = None
            break
        r = ')' if l[0]=='(' else ']'
        del l[0]
        l = isSet(r,l)
    print('yes' if l==[] else 'no')

Solution (Python):


while True:
    s = raw_input()
    if s  == '.':
        break
    l = []
    a = 0
    for i in range(len(s)):
        if s[i] == '(' or s[i] == '[':
            l.append(s[i])
        elif s[i] == ')':
            if l and l[-1] == '(':
                l = l[:-1]
            else:
                a = 1
                break
        elif s[i] == ']':
            if l and l[-1] == '[':
                l = l[:-1]
            else:
                a = 1
                break
        else:
            pass
    if l or a == 1:
        print 'no'
        continue
    print 'yes'

Solution (Python):
def is_balanced(txt):
    round_brackets = []
    square_brackets = []

    for c in txt:
        try:
            if c == '(':
                round_brackets.append(len(square_brackets))
            elif c == ')':
                level = round_brackets.pop()
                if level != len(square_brackets):
                    return False
            elif c == '[':
                square_brackets.append(len(round_brackets))
            elif c == ']':
                level = square_brackets.pop()
                if level != len(round_brackets):
                    return False
        except IndexError:
            return False
    if len(round_brackets) == 0 and len(square_brackets)==0:
        return True
    else:
        return False


if __name__ == '__main__':
    
    
    while True:
        txt = input()
        if txt == '.':
            break

        
        ans = is_balanced(txt)

        
        if ans:
            print('yes')
        else:
            print('no')

Solution (Python):
def is_balanced(txt):
    """ ????????????????????????????????¨??????????????¶
    1. ????????????????????¬??§(???(???)?????????????????????????????¬??§(???)???)??????????????????????????????
    2. ????????????????§???¬??§(???[???)?????????????????????????§???¬??§(???]???)??????????????????????????????
    3. ?????????????????¬??§??????????????????????????¬??§????????????
    4. ??¬??§???????????????????????§???????????????????????????????????¬??§????????°?????¬??§??¨?????????????????¨????????????
    ??? ????????????????????????????????¬??§???????????§?????£??????pop()??¨??????????????????????????????????????????????????????????????????????????¨

    5. ?????????????????????????????¬??§??¨?????¬??§????????????????????????????????????????????????????????§ ??¬??§??????????????????????????????
    ??? '( [  ) ]' ??????????????????????????????????????????NG
    """
    round_brackets = []  
    square_brackets = []  

    for c in txt:
        if c == '(':
            round_brackets.append(len(square_brackets))  
        elif c == ')':
            try:  
                level = round_brackets.pop()
                if level != len(square_brackets):  
                    return False
            except IndexError:
                return False
        elif c == '[':
                square_brackets.append(len(round_brackets))
        elif c == ']':
            try:
                level = square_brackets.pop()
                if level != len(round_brackets):
                    return False
            except IndexError:
                return False

    
    if len(round_brackets) == 0 and len(square_brackets) == 0:
        return True
    else:
        return False


if __name__ == '__main__':
    
    
    while True:
        txt = input()
        if txt == '.':
            break

        
        ans = is_balanced(txt)

        
        if ans:
            print('yes')
        else:
            print('no')

Solution (Python):
import re
while True:
    s = input()
    if s=='.': break
    s = re.sub(u'[a-zA-Z .]+','',s)
    while '()' in s or '[]' in s:
        s = s.replace('()','').replace('[]','')
    print('yes' if len(s)==0 else 'no')

Solution (Python):
import re
while 1:
    s=input()
    if s=='.':break
    s=re.sub(u'[A-Za-z .]','',s)
    while '()' in s or '[]' in s:
        s=s.replace('()','').replace('[]','')
    print(['yes','no'][bool(s)])

Solution (Python):
rev = {"(":")", "[":"]"}

while True:
    s = input()
    if s == '.':
        break
    stack = []
    flag = True
    for i in s:
        if i == '(' or i == '[':
            stack.append(i)
        if i == ')' or i == ']':
            if len(stack) == 0 or rev[stack[-1]] != i:
                flag = False
                break
            else:
                stack.pop()
    if len(stack) == 0 and flag:
        print("yes")
    else:
        print("no")

Solution (Python):


if __name__ == '__main__':

    while True:
        s = input()
        if s == '.':
            break
        s2 = ''
        for i in s:
            if i == '(' or i == ')' or i == '[' or i == ']':
                s2 += i
        cnt = 0
        while cnt <= 50:
            s2 = s2.replace('()', '')
            s2 = s2.replace('[]', '')
            cnt += 1
        if len(s2) != 0:
            print('no')
        else:
            print('yes')

Solution (Python):
import sys

def solve():
    while 1:
        s = sys.stdin.readline().rstrip()

        if s == '.':
            return

        stack = []

        for ch in s:
            if ch == '(' or ch == '[':
                stack.append(ch)
            elif ch == ')':
                if stack and stack[-1] == '(':
                    stack.pop()
                else:
                    print('no')
                    break
            elif ch == ']':
                if stack and stack[-1] == '[':
                    stack.pop()
                else:
                    print('no')
                    break
        else:
            if not stack:
                print('yes')
            else:
                print('no')
                
if __name__ == '__main__':
    solve()

Solution (Python):
while 1:
    a = list(input())
    if a == ['.']:break
    stack = []
    for i in a:
        if i =='(' :stack.append('(')
        elif i == '[':stack.append('[')
        elif i ==')':
            if len(stack) ==0:
                stack.append(0)
                break
            b = stack.pop()
            if b =='(':pass
            else:
                stack.append(b)
                break

        elif i ==']':
            if len(stack) == 0:
                stack.append(0)
                break
            b = stack.pop()
            if b =='[':pass
            else:
                stack.append(b)
                break
    if len(stack) == 0 and len(stack) == 0:print("yes")
    else:print("no")

Solution (Python):
def ok(s):
    stack = []
    for c in s:
        if c in '([':
            stack.append(c)
        elif c in ')]':
            if len(stack) == 0: return False
            e = stack.pop()
            if (e+c) not in ('()','[]'):
                return False
    return False if stack else True

while True:
    S = input()
    if S == '.': break
    print('yes' if ok(S) else 'no')

Solution (Python):
def makeBlackets(strings):
    blackets=[]
    for i in range(len(strings)):
        char=strings[i]
        if char=='[' or char=='(' or char==']' or char==')':
            blackets.append(char)
    return blackets
def isBalancedNum(blackets):
    lB=0
    rB=0
    for i in range(len(blackets)):
        if blackets[i]==')' or blackets[i]==']':
            lB+=1
        else:
            rB+=1
    
    return lB==rB
def isBalanced(blackets):
    while len(blackets)!=0:
        if "()" in blackets:
            blackets=blackets.replace("()","")
        elif "[]" in blackets:    
            blackets=blackets.replace("[]","")
        else:
            break
    return len(blackets)==0
while True:
    strings=input()
    if strings==".":
        break
    blacket=makeBlackets(strings)
    blackets=''.join(blacket)
    if isBalancedNum(blackets):
        if isBalanced(blackets):
            print("yes")
        else:
            print("no")
    else:
        print("no")

Solution (Python):
def is_balanced(s):
    stack = []
    for c in s:
        if c == '(' or c == '[':
            stack.append(c)
        elif c == ')':
            if not stack:
                return "no"
            elif stack[-1] == '(':
                stack.pop()
            else:
                return "no"
        elif c == ']':
            if not stack:
                return "no"
            elif stack[-1] == '[':
                stack.pop()
            else:
                return "no"
    if stack:
        return "no"
    else:
        return "yes"

import sys

file_input = sys.stdin

lines = file_input.readlines()
lines = lines[:-1]
 
print('\n'.join(map(is_balanced, lines)))

Solution (Python):
while (True):
  st = input()
  if st == ".":
      quit()
  han = []
  ans = "yes"
  for i in st:
    if i == "(":
      han.append("maru")
    elif i == "[":
      han.append("kaku")
    elif i == ")":
      if han == []:
        ans = "no"
        break
      elif han[-1] == "maru":
        del han[-1]
      else:
        ans = "no"
        break
    elif i == "]":
      if han == []:
        ans = "no"
        break
      elif han[-1] == "kaku":
        del han[-1]
      else:
        ans = "no"
        break
  if ans == "no" or han != []:
    print("no")
  else:
    print(ans)

Solution (Python):
while True:
    s = input()
    if s == ".":
        break
    k = []
    for c in s:
        if c == "(":
            k.append(c)
        elif c == "[":
            k.append(c)
        elif c == ")":
            if k != [] and k[-1] == "(":
                k.pop()
            else:
                print("no")
                break
        elif c == "]":
            if k != [] and k[-1] == "[":
                k.pop()
            else:
                print("no")
                break
    else:
        if k == []:
            print("yes")
        else:
            print("no")

Solution (Python):
while True:
    s = input()
    if s == ".":
        break

    stack = list()
    judge = True

    for c in s:
        if c == '(' or c == '[':
            stack.append(c)

        if c == ')':
            if stack and stack[-1] == '(':
                stack.pop()
            else:
                judge = False
                break

        if c == ']':
            if stack and stack[-1] == '[':
                stack.pop()
            else:
                judge = False
                break

    if judge and not stack:
        print("yes")
    else:
        print("no")

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    dd = [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]

    while True:
        s = S()
        if s == '.':
            break
        mc = 0
        kc = 0
        f = True
        st = []
        for c in s:
            if c == '(':
                mc += 1
                st.append(1)
            elif c == ')':
                if mc < 1 or st[-1] == 0:
                    f = False
                    break
                mc -= 1
                st = st[:-1]
            elif c == '[':
                kc += 1
                st.append(0)
            elif c == ']':
                if kc < 1 or st[-1] == 1:
                    f = False
                    break
                kc -= 1
                st = st[:-1]
        if f and mc == 0 and kc == 0:
            rr.append('yes')
        else:
            rr.append('no')

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):
import re
import sys
sys.setrecursionlimit(10**6)


def main():
  s=input()
  if s == ".":
    return
  s=re.sub(r'[a-zA-Z .]+',"",s)
  while "()" in s or "[]" in s:
    s=s.replace("()","")
    s=s.replace("[]","")
  if s == "":
    print("yes")
  else:
    print("no")
  main()
  return

main()

Solution (Python):
while 1:
    k=[]
    l=input()
    if l==".":break
    f=False
    for i in l:
        if f:break
        if i=="[" or i=="(":
            k.append(i)
        elif i=="]":
            try:
                if k.pop()!="[":
                    f=True
            except:
                f=True
        elif i==")":
            try:
                if k.pop()!="(":
                    f=True
            except:
                f=True
    if not k and not f:print("yes")
    elif k or f:print("no")

Solution (Python):
while 1:
    s=raw_input()
    if(s=='.'):
        break
    stack=[]
    ok=True
    for c in s:
        if(c=='(' or c=='['):
            stack.append(c)
        elif(c==')' or c==']'):
            if(not stack):
                ok=False
                break
            top=stack.pop()
            cor={')':'(', ']':'['}
            if(top!=cor[c]):
                ok=False
                break
    if(stack):
       ok=False 
    if(ok):
        print 'yes'
    else:
        print 'no'

Solution (Python):
import re

while 1:
    s=raw_input()
    if(s=='.'):
        break
    s = re.sub('[^][()]', '', s)
    while (1):
        tmp=s
        s = re.sub('\[\]', '', s)
        s = re.sub('\(\)', '', s)
        if (tmp==s):
            break
    if(not s):
        print 'yes'
    else:
        print 'no'

Solution (Python):
import re

while 1:
    s=raw_input()
    if(s=='.'):
        break

    after=''
    needed=['(',')','[',']']
    for c in s:
        if(c in needed):
            after+=c
    s=after
    while (1):
        tmp=s


        s=s.replace('[]','')
        s=s.replace('()','')
        if (tmp==s):
            break
    if(not s):
        print 'yes'
    else:
        print 'no'

Solution (Python):
import sys
R = lambda:map(int,raw_input().split())

while 1:
    s = raw_input()
    if s == '.': break
    left = []
    flag = False
    for i in xrange(len(s)):
        
        if (s[i] == '(') or (s[i] == '['):
            left.append(s[i])
        elif s[i] == ')':
            if not left:
                flag = True
                break
            elif left[len(left)-1] == '(':
                left.pop()
            else:
                flag = True
                break
        elif s[i] == ']':
            if not left:
                flag = True
                break
            elif left[len(left)-1] == '[':
                left.pop()
            else:
                flag = True
                break
        
    
    if left: flag = True
    if not flag: print 'yes'
    else: print 'no'

Solution (Python):
import sys


pair = {')': '(', ']': '['}
for line in sys.stdin:
    line = line.rstrip()
    if line == '.':
        break
    st = []
    for c in line:
        if c == '(' or c == '[':
            st.append(c)
        elif c in pair:
            if len(st) > 0 and st[-1] == pair[c]:
                st.pop()
            else:
                print 'no'
                break
    else:
        if len(st) == 0:
            print 'yes'
        else:
            print 'no'

Solution (Python):
while True:
    s=raw_input()
    if s==".":break
    ST=[]
    flag=True
    for i in s:
        if i=="(" or i=="[":
            ST.append(i)
        elif i==")":
            if len(ST)>0 and ST[-1]=="(":
                ST.pop()
            else:
                flag=False
                break
        elif i=="]":
            if len(ST)>0 and ST[-1]=="[":
                ST.pop()
            else:
                flag=False
                break
    print "yes" if flag and len(ST)==0 else "no"

Solution (Python):
dic={")":"(","]":"["}
while True:
    s=raw_input()
    if s==".":break
    ST=[]
    flag=True
    for i in s:
        if i=="(" or i=="[":
            ST.append(i)
        elif i in dic:
            if len(ST)>0 and ST[-1]==dic[i]:
                ST.pop()
            else:
                flag=False
                break
    print "yes" if flag and len(ST)==0 else "no"

Solution (Python):
while True:
  s = raw_input()
  if s == '.': break

  ns = ''
  for c in s:
    if c in ['(', ')', '[', ']']:
      ns += c

  while True:
    tmp = ns
    ns = ns.replace('[]', '')
    ns = ns.replace('()', '')
    if (tmp == ns): break

  print 'no' if ns else 'yes'

Solution (Python):
while 1:
	inp = raw_input()
	if inp == ".": break
	a = ""
	for i in inp:
		if i == "(" or i == ")" or i == "[" or i == "]":
			a += i
	while len(a) > 0:
		if   "()" in a: a = a.replace("()","")
		elif "[]" in a: a = a.replace("[]","")
		else: break
	print "yes" if len(a) == 0 else "no"

Solution (Python):
while 1:
	a = raw_input()
	if a == ".": break
	a = "".join([i for i in a if i in "()[]"])
	while len(a) > 0:
		if   "()" in a: a = a.replace("()","")
		elif "[]" in a: a = a.replace("[]","")
		else: break
	print "yes" if len(a) == 0 else "no"

Solution (Python):
def parse(s, left, target_stack):
  if s[left] == "[":
    target_stack.append("]")
  if s[left] == "(":
    target_stack.append(")")
  
  if s[left] == "]":
    if target_stack and target_stack[-1] == "]":
      target_stack.pop()
    else:
      return False

  if s[left] == ")":
    if target_stack and target_stack[-1] == ")":
      target_stack.pop()
    else:
      return False

  left += 1
  if left == len(s):
    if target_stack:
      return False
    else:
      return True

  else:
    return parse(s, left, target_stack)

while True:
  s = input()
  if s == ".":
    break

  if parse(s, 0, []):
    print("yes")
  else:
    print("no")

Solution (Python):
while True:
    S = input()
    if S == ".":
        break
    
    brackets = []
    closed = True
    for c in S:
        if c in "([":
            brackets.append(c)
        elif c == ")":
            if not brackets or brackets[-1] != "(":
                closed = False
                break
            else:
                brackets.pop()
        elif c == "]":
            if not brackets or brackets[-1] != "[":
                closed = False
                break
            else:
                brackets.pop()
    
    if brackets:
        closed = False
        
    if closed:
        print("yes")
    else:
        print("no")

Solution (Python):
from collections import deque
while True:
    S = input()
    if S == ".":
        break
    que = deque()
    flag = True
    for s in S:
        if s == "(":
            que.append(1)
        elif s == "[":
            que.append(-1)
        elif s == ")" or s == "]":
            if len(que) <= 0:
                flag = False
                break
            temp1 = -1 if s == ")"else 1
            temp2 = que.pop()
            if temp1 + temp2 != 0:
                flag = False
                break
    if len(que) != 0:
        flag = False

    if flag:
        print("yes")
    else:
        print("no")