human
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times.
Base Case 1.
Count of ' ( ' and ').
Base Case 2.
Store the count of moves required to make a valid parenthesis.
Traverse the string.
Increment cnt if opening bracket has occurred.
Otherwise , decrement cnt by 1.
Decrement cnt by 1.
If cnt is negative.
Update the cnt.
Increment the ans.
If ans is at most K , then print Yes . Otherwise print No.
Driver Code"
How can the above be solved in Python?
 Write me a code to check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times and the other steps required?
assistant
#Here is code to check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times and the other steps required.
 def minimumMoves ( s , n , k ) :
	if ( n & 1 ) :
		print ( " No " )
		return
'
	countOpen = s . count ( ' ( ' )
	countClose = s . count ( ' ) ' )
	if ( countOpen != countClose ) :
		print ( " No " )
		return
	ans = 0
	cnt = 0
	for i in range ( n ) :
		if ( s [ i ] == ' ( ' ) :
			cnt += 1
		else :
			cnt -= 1
			if ( cnt < 0 ) :
				cnt = 0
				ans += 1
	if ( ans <= k ) :
		print ( " Yes " )
	else :
		print ( " No " )
if __name__ == " _ _ main _ _ " : S = " ) ( "
	K = 1
	minimumMoves ( S , len ( S ) , K )