Lexicographically smallest string whose hamming distance from given string is exactly K | javascript program to find Lexicographically smallest string whose hamming distance from given string is exactly K ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; str2 [ i ] = ' a ' ; ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driven Program
function findString ( str , n , k ) { if ( k == 0 ) { document . write ( str ) ; ; return ; } var str2 = str ; var p = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str2 . charAt ( i ) != ' ' ) { str2 = str2 . substring ( 0 , i ) + ' ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } if ( p < k ) { for ( i = n - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == ' ' ) { str2 = str2 . substring ( 0 , i ) + ' ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } document . write ( str2 ) ; } var str = " " ; var n = str . length ; var k = 2 ; findString ( str , n , k ) ;