human
Check if concatenation of two strings is balanced or not | Check if given string is balanced bracket sequence or not ..
If current bracket is an opening bracket push it to stack ..
If current bracket is a closing bracket then pop from stack if it is not empty . If stack is empty then sequence is not balanced ..
If stack is not empty , then sequence is not balanced ..
Function to check if string obtained by concatenating two bracket sequences is balanced or not ..
Check if s1 + s2 is balanced or not ..
Check if s2 + s1 is balanced or not ..
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to check if concatenation of two strings is balanced or not and the other steps required.
 def isBalanced ( s ) :
	st = list ( )
	n = len ( s )
	for i in range ( n ) :
		if s [ i ] == ' ( ' :
			st . append ( s [ i ] )
		else :
			if len ( st ) == 0 :
				return False
			else :
				st . pop ( )
	if len ( st ) > 0 :
		return False
	return True
def isBalancedSeq ( s1 , s2 ) :
	if ( isBalanced ( s1 + s2 ) ) :
		return True
	return isBalanced ( s2 + s1 )
if __name__ == " _ _ main _ _ " : s1 = " ) ( ) ( ( ) ) ) ) "
	s2 = " ( ( ) ( ( ) ( "
	if isBalancedSeq ( s1 , s2 ) :
		print ( " Balanced " )
	else :
		print ( " Not ▁ Balanced " )