Convert to a string that is repetition of a substring of k length | Returns True if S can be converted to a with k repeated subs after replacing k characters . ; Length of must be a multiple of k ; Map to store s of length k and their counts ; If is already a repetition of k subs , return True . ; If number of distinct subs is not 2 , then not possible to replace a . ; One of the two distinct must appear exactly once . Either the first entry appears once , or it appears n / k - 1 times to make other sub appear once . ; Driver code
def check ( S , k ) : NEW_LINE INDENT n = len ( S ) NEW_LINE if ( n % k != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT mp = { } NEW_LINE for i in range ( 0 , n , k ) : NEW_LINE INDENT mp [ S [ i : k ] ] = mp . get ( S [ i : k ] , 0 ) + 1 NEW_LINE DEDENT if ( len ( mp ) == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( len ( mp ) != 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in mp : NEW_LINE INDENT if i == ( n // k - 1 ) or mp [ i ] == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if check ( " abababcd " , 2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT