human
Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | Function to count numbers.
Count the number of ' a ' , ' b ' and ' c ' in string.
If equal previously.
If not a multiple of 3.
Increase the number of a ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' b ' and c.
Check if it is ' b ' and it more than n / 3.
Check if it is ' c ' and it more than n / 3.
Increase the number of b ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' c.
Check if it is ' c ' and it more than n / 3.
Increase the number of c 's from back.
Check if it is ' a ' and it more than n / 3.
Increase the number of b 's from back.
Check if it is ' a ' and it more than n / 3.
Increase the number of c 's from back.
Check if it is ' b ' and it more than n / 3.
Driver Code"
How can the above be solved in Python?
 ... Write me a code to minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it and the other steps required?
assistant
#Here is code to minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it and the other steps required.
 def lexoSmallest ( s , n ) :
	ca = 0
	cb = 0
	cc = 0
	for i in range ( n ) :
		if ( s [ i ] == ' a ' ) :
			ca += 1
		elif ( s [ i ] == ' b ' ) :
			cb += 1
		else :
			cc += 1
	if ( ca == cb and cb == cc ) :
		return s
	cnt = n // 3
	if ( cnt * 3 != n ) :
		return " - 1"
	i = 0
	while ( ca < cnt and i < n ) :
		if ( s [ i ] == ' b ' and cb > cnt ) :
			cb -= 1
			s [ i ] = ' a '
			ca += 1
		elif ( s [ i ] == ' c ' and cc > cnt ) :
			cc -= 1
			s [ i ] = ' a '
			ca += 1
		i += 1
	i = 0
'
	while ( cb < cnt and i < n ) :
		if ( s [ i ] == ' c ' and cc > cnt ) :
			cc -= 1
			s [ i ] = '1'
			cb += 1
		i += 1
	i = n - 1
	while ( cc < cnt and i >= 0 ) :
		if ( s [ i ] == ' a ' and ca > cnt ) :
			ca -= 1
			s [ i ] = ' c '
			cc += 1
		i -= 1
	i = n - 1
	while ( cb < cnt and i >= 0 ) :
		if ( s [ i ] == ' a ' and ca > cnt ) :
			ca -= 1
			s [ i ] = ' b '
			cb += 1
		i -= 1
	i = n - 1
	while ( cc < cnt and i >= 0 ) :
		if ( s [ i ] == ' b ' and cb > cnt ) :
			cb -= 1
			s [ i ] = ' c '
			cc += 1
		i -= 1
	return s
s = " aaaaaa "
n = len ( s )
print ( * lexoSmallest ( list ( s ) , n ) , sep = " " )