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):
while True :
    s = input()
    tmp = []
    if s == '.' :
        break
    ans = True
    for i in range(len(s)) :
        if s[i] == '(' :
            tmp.append(s[i])
        elif s[i] == '[' :
            tmp.append(s[i])
        elif s[i] == ')' :
            if '(' not in tmp :
                ans = False
                break
            elif tmp[-1] == '(' :
                tmp.pop(-1)
            else :
                ans = False
                break
        elif s[i] == ']' :
            if '[' not in tmp :
                ans = False
                break
            elif tmp[-1] == '[' :
                tmp.pop(-1)
            else :
                ans = False
                break
    if len(tmp) != 0 or ans == False :
        print('no')
    else :
        print('yes')

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

    ans = True
    stack = []
    for i in s:
        if i == '[' or i == '(':
            tmp = 1
            if i == '[':
                tmp = -1
            if not len(stack) or stack[-1] * tmp < 0:
                stack.append(tmp)
            else:
                stack[-1] += tmp

        if i == ']' or i == ')':
            tmp = 1
            if i == ']':
                tmp = -1
            if not len(stack) or stack[-1] * tmp < 0:
                ans = False
                break
            else:
                stack[-1] -= tmp
                if not stack[-1]:
                    stack = stack[0:-1]

    if len(stack):
        ans = False

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

Solution (Python):
from collections import deque

def solve(s):
    dq = deque()
    for i in range(len(s)):
        if   s[i]=='(':dq.append(s[i])
        elif s[i]=='[':dq.append(s[i])
        elif s[i]==')':
            if len(dq)==0:return ('no')
            a = dq.pop()
            if a!='(':return ('no')
        elif s[i]==']':
            if len(dq)==0:return ('no')
            a = dq.pop()
            if a!='[':return ('no')
    if len(dq)==0:return ('yes')
    return ('no')

def main():
    while True:
        s = input()
        if s == '.':break
        print (solve(s))


if __name__ == '__main__':
    main()

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

    st = []
    is_valid = True
    for c in s:
        if c == "(": st.append(c)
        if c == "[": st.append(c)
        if c == ")":
            if (not st) or st[-1] != "(":
                is_valid = False
                break
            else:
                st.pop()
        if c == "]":
            if (not st) or st[-1] != "[":
                is_valid = False
                break
            else:
                st.pop()
    if st:
        is_valid = False

    print( "yes" if is_valid else "no" )

Solution (Python):
from collections import Counter
ans = []
while 1:
    s = list(str(input()))
    S = "".join(s)
    if S[0] == ".":
        break
    flag = 0
    stack = []
    for i in range(len(S)):
        if S[i] == "(":
            stack.append(S[i])
        if S[i] == "[":
            stack.append(S[i])
        if S[i] == ")":
            stack.append(S[i])
        if S[i] == "]":
            stack.append(S[i])
    c = Counter(stack)
    if c["{"] != c["}"]:
        print("no")
        flag = 2
    elif c["("] != c[")"]:
        print("no")
        flag = 2
    elif c["["] != c["]"]:
        print("no")
        flag = 2

    m = len(stack)
    if flag == 0:
        while len(stack) > 0:
            if flag == 1:
                break
            if stack[0] == ")" or stack[0] == "]":
                print("no")
                flag = 1
                break
            for i in range(1, m):
                if stack[i] == ")":
                    if stack[i - 1] == "(":
                        stack.pop(i)
                        stack.pop(i - 1)
                        m = len(stack)
                        break
                    else:
                        print("no")
                        flag = 1
                        break
                if stack[i] == "]":
                    if stack[i - 1] == "[":
                        stack.pop(i)
                        stack.pop(i - 1)
                        m = len(stack)
                        break
                    else:
                        print("no")
                        flag = 1
                        break
            if flag == 1:
                break
        if flag == 0:
            print("yes")

Solution (Python):
while True:
    s = input()
    if s == ".":
        break
    L = []
    f = 0
    for c in s:
        if c in "([":
            L.append(c)
        elif c == ")":
            if len(L) > 0 and L[-1] == "(":
                L.pop()
            else:
                f += 1
                break
        elif c == "]":
            if len(L) > 0 and L[-1] == "[":
                L.pop()
            else:
                f += 1
                break
    
    if len(L) == 0 and f == 0:
        print("yes")
    else:
        print("no")

Solution (Python):
while True :
    S = input()
    
    if(S == ".") :
        break
    
    else :
        B = list()
        b = -1
        for i in range(len(S)) :
            if(S[i] == "(" or S[i] == "[") :
                B.append(S[i])
                b += 1
                
            elif(S[i] == ")") :
                if(b > -1) :
                    if(B[b] == "(") :
                        del B[b]
                        b -= 1
                    else :
                        b = 0
                        break
                else :
                    b = 0
                    break
                
            elif(S[i] == "]") :
                if(b > -1) :
                    if(B[b] == "[") :
                        del B[b]
                        b -= 1
                    else :
                        b = 0
                        break
                else :
                    b = 0
                    break
                
            else :
                pass
            
        if(b == -1) :
            print("yes")
        else :
            print("no")

Solution (Python):
while True:
    S = input()
    if S==".":
        break
    a = [-1]
    b = [-1]
    ans = "yes"
    for i in range(len(S)):
        s = S[i]
        if s=="(":
            a.append(i)
        if s=="[":
            b.append(i)
        if s==")":
            if len(a)==1 or a[len(a)-1]<b[len(b)-1]:
                ans = "no"
                break
            else:
                del a[len(a)-1]
        if s=="]":
            if len(b)==1 or b[len(b)-1]<a[len(a)-1]:
                ans = "no"
                break
            else:
                del b[len(b)-1]
    if len(a)!=1 or len(b)!=1:
        ans = "no"
    print(ans)

Solution (Python):
def main(s):
    s = "".join(list(s.split()))
    bra1 = 0
    bra2 = 0
    for i in range(len(s)):
        if s[i] != "(" and s[i] != ")" and s[i] != "[" and s[i] != "]":
            continue
        if s[i] == "(":
            if bra1 == 0:
                bra10 = i
            bra1 += 1
        if s[i] == ")":
            bra1 -= 1
            if bra1 == 0:
                if not main(s[bra10 + 1:i]):
                    return False
        if s[i] == "[":
            if bra2 == 0:
                bra20 = i
            bra2 += 1
        if s[i] == "]":
            bra2 -= 1
            if bra2 == 0:
                if not main(s[bra20 + 1:i]):
                    return False
        if bra1 < 0 or bra2 < 0:
            return False
    if bra1 > 0 or bra2 > 0:
        return False
    return True

while 1:
    s = input()
    if s == ".":
        break
    print("yes" if main(s) else "no")

Solution (Python):
"世界の天秤"
pattern = ('()', '[]')

while True:
    S = input()
    if S == '.':
        break
    sentence_list = list(S)
    target = ('(', ')', '[', ']')
    for i in range(len(sentence_list)):
        if sentence_list[i] not in target:
            sentence_list[i] = ''

    sentence = ''.join(sentence_list).strip()

    while True:
        sentence_len = len(sentence)
        sentence = sentence.replace('[]', '')
        sentence = sentence.replace('()', '')
        if sentence_len == len(sentence):
            break

    if len(sentence) == 0:
        print('yes')
    else:
        print('no')

Solution (Python):


import re
from collections import deque

while 1:
    x = list(input())
    if x[0] == ".":
        break

    stack = deque()
    f = 1
    for i in range(len(x)):
        for j in range(len(x[i])):
            if x[i][j] == '(':
                stack.append('(')
            elif x[i][j] == '[':
                stack.append('[')
            elif x[i][j] == ')':
                if len(stack) == 0:
                    f = 0
                elif stack.pop() != '(':
                    f = 0
            elif x[i][j] == ']':
                if len(stack) == 0:
                    f = 0
                elif stack.pop() != '[':
                    f = 0
    if len(stack) != 0:
        f = 0
    if f:
        print('yes')
    else:
        print('no')

Solution (Python):
import sys

sys.setrecursionlimit(1000000)


class St():
    def __init__(self, s):
        self.s = s
        self.i = 0

def expression(st, num):
    while (not st.s[st.i]=="."):
        if (st.s[st.i] == "("):
            if (not factor1(st)):
                return False

        elif (st.s[st.i] == "["):
            if (not factor2(st)):
                return False

        elif (not st.s[st.i] in [")", "]"]):
            st.i += 1

        else:
            if ((num==1) and (st.s[st.i]==")")) or ((num==2) and (st.s[st.i]=="]")):
                break
            else:
                return False
        if (st.i >= len(st.s)):
            return False
    return True
    
    
def factor1(st):
    st.i += 1
    if (st.s[st.i]=="."):
        return False
    if (expression(st, 1)):
        st.i += 1
        return True
    return False

def factor2(st):
    st.i += 1
    if (st.s[st.i]=="."):
        return False
    if (expression(st, 2)):
        st.i += 1
        return True
    return False


def solve(s):
    st = St(s)
    return expression(st, 0)

def main():
    ans = []
    while True:
        s = input()
        if (s=="."):
            break
        ans.append(solve(s))
    for i in ans:
        print("yes" if i else "no")


main()

Solution (Python):
from collections import deque

def check(line):
    Q = deque([])
    for s in line:
        if s in ['(', '[']:
            Q.append(s)
        elif s in [')', ']']:
            if len(Q) == 0:
                return 0
            p = Q.pop()
            if s == ')' and p != '(':
                return 0
            elif s == ']' and p != '[':
                return 0
            else:
                continue
    else:
        if len(Q) != 0:
            return 0
        else:
            return 1



ans = []
while 1:
    line = input()
    if line == '.': break
    ans += ['yes' if check(line) else 'no']

print('\n'.join(ans))

Solution (Python):
while True:
    s = input()
    if s == ".":
        break
    check = []
    ok = True
    for i in range(len(s)):
        if s[i] in ["(", "["]:
            check.append(s[i])
        elif s[i] == ")":
            if len(check) <= 0:
                ok = False
                break
            elif check.pop() != "(":
                ok = False
                break
        elif s[i] == "]":
            if len(check) <= 0:
                ok = False
                break
            elif check.pop() != "[":
                ok = False
                break
    if len(check) > 0:
        ok = False
    
    if ok:
        print("yes")
    else:
        print("no")

Solution (Python):
def main():
    while True:
        line = input()
        if line == ".": break
        line = line.split()

        k_list = []
        for item in line:
            for j in item:
                if j == "(" or j == ")" or j == "[" or j == "]":
                    k_list.append(j)

        while True:
            bef = k_list.copy()
            k_list = rem_mk(k_list)
            k_list = rem_kk(k_list)
            aft = k_list.copy()
            
            if bef == aft: break

        if k_list == []: print("yes")
        else: print("no")

    return

def rem_mk(a):
    for i in range(len(a) - 1):
        if a[i] == "(" and a[i + 1] == ")":
            del a[i + 1]
            del a[i]
            return a
    return a
def rem_kk(a):
    for i in range(len(a) - 1):
        if a[i] == "[" and a[i + 1] == "]":
            del a[i + 1]
            del a[i]
            return a
    return a
main()

Solution (Python):
import re
while 1:
    s=input()
    if s==".":break
    s=s.replace(" ","").replace(".","")
    st=re.sub("[a-zA-Z]","",s)
    a1,a2,b1,b2=0,0,0,0
    while 1:
        if "()" in st or "[]" in st:st=st.replace("()","").replace("[]","")
        else:break
    print(["no","yes"][st==""])

Solution (Python):
while True:
    x = raw_input()
    if x == ".":break
    a = ""
    for s in x:
        if s in ["(",")","[","]"]:
            a += s
    while True:
        if "()" in a or "[]" in a:
            a = a.replace('[]', '')
            a = a.replace('()', '')
        else:
            break
    if a =="":
        print "yes"
    else:
        print "no"

Solution (Python):
while 1:
    s=input()
    if s=='.':
        break
    stk=[]
    try:
        for c in s:
            if c in '([':
                stk.append(c)
            elif c == ')' and stk[-1]=='(':
                del stk[-1]
            elif c == ']' and stk[-1]=='[':
                del stk[-1]
            elif c in '()[]':
                print('no')
                break
        else:
            if len(stk)==0:
                print('yes')
            else:
                print('no')
    except:
        print('no')