human
Lexicographically smallest permutation of a string that can be reduced to length K by removing K | Function to count the number of zeroes present in the string.
Traverse the string.
Return the count.
Function to rearrange the string s . t the string can be reduced to a length K as per the given rules.
Distribute the count of 0 s and 1 s in segment of length 2 k.
Store string that are initially have formed lexicographically smallest 2 k length substring.
Store the lexicographically smallest string of length n that satisfy the condition.
Insert temp_str into final_str ( n / 2 k ) times and add ( n % 2 k ) characters of temp_str at end.
Return the final string.
Function to reduce the string to length K that follows the given conditions.
If the string contains either 0 s or 1 s then it always be reduced into a K length string.
If the string contains only 0 s 1 s then it always reduces to a K length string.
If K = 1.
Check whether the given string is K reducing string or not.
Otherwise recursively find the string.
Driver Code.
Function Call"
Solution in Python:
assistant
#Here is code to lexicographically smallest permutation of a string that can be reduced to length K by removing K and the other steps required.
 def count_zeroes ( n , str ) :
	cnt = 0
	for i in range ( 0 , len ( str ) ) :
		if ( str [ i ] == '0' ) :
		cnt += 1
	return cnt
def kReducingStringUtil ( n , k , str , no_of_zeroes ) :
	zeroes_in_2k = ( ( ( no_of_zeroes ) * ( 2 * k ) ) // n )
	ones_in_2k = 2 * k - zeroes_in_2k
	temp_str = " "
	for i in range ( 0 , ( zeroes_in_2k ) // 2 ) :
		temp_str += '0'
	for i in range ( 0 , ( ones_in_2k ) ) :
		temp_str += '1'
	for i in range ( 0 , ( zeroes_in_2k ) // 2 ) :
		temp_str += '0'
	final_str = " "
	for i in range ( 0 , n // ( 2 * k ) ) :
		final_str += ( temp_str )
	for i in range ( 0 , n % ( 2 * k ) ) :
		final_str += ( temp_str [ i ] )
	return final_str
def kReducingString ( n , k , str ) :
	no_of_zeroes = count_zeroes ( n , str )
	no_of_ones = n - no_of_zeroes
	if ( no_of_zeroes == 0 or no_of_zeroes == n ) :
		return str
	if ( k == 1 ) :
		if ( no_of_zeroes == 0 or no_of_zeroes == n ) :
		return str
		else :
		return " Not ▁ Possible "
	check = 0
	for i in range ( ( n // k ) , n , ( n // k ) ) :
		if ( no_of_zeroes == i or no_of_ones == i ) :
		check = 1
		break
	if ( check == 0 ) :
		return " Not ▁ Possible "
	return kReducingStringUtil ( n , k , str , no_of_zeroes )
if __name__ == ' _ _ main _ _ ' :
	str = "0000100001100001"
	K = 4 ;
	N = len ( str )
	print ( kReducingString ( N , K , str ) )