Sub | Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; Sub - strings of length 26 ; To store frequency of each character ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code
function valid ( cnt ) { for ( var i = 0 ; i < 26 ; i ++ ) { if ( cnt [ i ] >= 2 ) return false ; } return true ; } function getGoodString ( ss , n ) { var s = ss . split ( " " ) ; if ( n < 26 ) return " " ; for ( var i = 25 ; i < n ; i ++ ) { var cnt = new Array ( 26 ) . fill ( 0 ) ; for ( var j = i ; j >= i - 25 ; j -- ) { cnt [ s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } if ( valid ( cnt ) ) { var cur = 0 ; while ( cnt [ cur ] > 0 ) cur ++ ; for ( var j = i - 25 ; j <= i ; j ++ ) { if ( s [ j ] === " " ) { s [ j ] = String . fromCharCode ( cur + " " . charCodeAt ( 0 ) ) ; cur ++ ; while ( cnt [ cur ] > 0 ) cur ++ ; } } return s . join ( " " ) ; } } return " " ; } var s = " " ; var n = s . length ; document . write ( getGoodString ( s , n ) ) ;