Suppose we have two bracket sequences s and t with only these characters '(' and ')'. We have to check whether the concatenated string of s and t is balanced or not. The concatenation can be done by s | t or t | s.
So, if the input is like s = "()()))", t = "()(()(", then the output will be True because if we concatenate t | s, then we will get "()(()(()()))", which is balanced.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def is_balanced_parenthesis(string): stack =  for i in range(len(string)): if string[i] == '(': stack.append(string[i]) else: if len(stack) == 0: return False else: stack.pop() if len(stack) > 0: return False return True def solve(s, t): if is_balanced_parenthesis(s + t): return True return is_balanced_parenthesis(t + s) s = "()()))" t = "()(()(" print(solve(s, t))