human
Minimum replacements to make adjacent characters unequal in a ternary string | Function to count the number of minimal replacements.
Find the length of the string.
Iterate in the string.
Check if adjacent is similar.
If not the last pair.
Check for character which is not same in i + 1 and i - 1.
Last pair.
Check for character which is not same in i - 1 index.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimum replacements to make adjacent characters unequal in a ternary string and the other steps required.
 def countMinimalReplacements ( s ) :
	n = len ( s )
	cnt = 0
	for i in range ( 1 , n ) :
		if ( s [ i ] == s [ i - 1 ] ) :
			cnt += 1 ;
			if ( i != ( n - 1 ) ) :
				s = list ( s )
				for j in "012" :
					if ( j != s [ i + 1 ] and j != s [ i - 1 ] ) :
						s [ i ] = j
						break
				s = ' ' . join ( s )
			else :
				s = list ( s )
				for k in "012" :
					if ( k != s [ i - 1 ] ) :
						s [ i ] = k
						break
				s = ' ' . join ( s )
	return cnt
if __name__ == ' _ _ main _ _ ' :
	s = "201220211"
	print ( countMinimalReplacements ( s ) )