human
Lexicographically smallest K | Function to find lexicographically smallest subsequence of size K.
Length of string.
Stores the minimum subsequence.
Traverse the string S.
If the stack is empty.
Iterate till the current character is less than the the character at the top of stack.
If stack size is < K.
Push the current character into it.
Stores the resultant string.
Iterate until stack is empty.
Reverse the string.
Print the string.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to lexicographically smallest K and the other steps required.
 def smallestSubsequence ( S , K ) :
	N = len ( S )
	answer = [ ]
	for i in range ( N ) :
		if ( len ( answer ) == 0 ) :
			answer . append ( S [ i ] )
		else :
			while ( len ( answer ) > 0 and ( S [ i ] < answer [ len ( answer ) - 1 ] ) and ( len ( answer ) - 1 + N - i >= K ) ) :
				answer = answer [ : - 1 ]
			if ( len ( answer ) == 0 or len ( answer ) < K ) :
				answer . append ( S [ i ] )
	ret = [ ]
	while ( len ( answer ) > 0 ) :
		ret . append ( answer [ len ( answer ) - 1 ] )
		answer = answer [ : - 1 ]
	ret = ret [ : : - 1 ]
	ret = ' ' . join ( ret )
	print ( ret )
if __name__ == ' _ _ main _ _ ' :
	S = " aabdaabc "
	K = 3
	smallestSubsequence ( S , K )