human
What is a code to find a valid parenthesis sequence of length K from a given valid parenthesis sequence and the other steps required given the following:
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | Function to find the subsequence of length K forming valid sequence.
Stores the resultant string.
Check whether character at index i is visited or not.
Traverse the string.
Push index of open bracket.
Pop and mark visited.
Increment count by 2.
Append the characters and create the resultant string.
Return the resultant string.
Driver Code.
Function call"
Solution in Python:
assistant
#Here is code to find a valid parenthesis sequence of length K from a given valid parenthesis sequence and the other steps required.
 def findString ( s , k ) :
	n = len ( s )
	ans = " "
	st = [ ]
	vis = [ False ] * n
	count = 0
	List < bool > vis ( n , false ) ;
	for i in range ( n ) :
		if ( s [ i ] == ' ( ' ) :
			st . append ( i )
		if ( count < k and s [ i ] == ' ) ' ) :
			vis [ st [ - 1 ] ] = 1
			del st [ - 1 ]
			vis [ i ] = True
			count += 2
	for i in range ( n ) :
		if ( vis [ i ] == True ) :
			ans += s [ i ]
	return ans
if __name__ == ' _ _ main _ _ ' :
	s = " ( ) ( ) ( ) "
	K = 2
	print ( findString ( s , K ) )