Modify string by inserting characters such that every K | Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver code
function fillString ( str , k ) { var s = str . split ( " " ) ; var mp = { } ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] !== " " ) { mp [ i % k ] = s [ i ] ; } } for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! mp . hasOwnProperty ( i % k ) ) { document . write ( - 1 ) ; return ; } s [ i ] = mp [ i % k ] ; } document . write ( s . join ( " " ) + " " ) ; } var S = " " ; var K = 4 ; fillString ( S , K ) ;