Modify string by inserting characters such that every K | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void fillString ( string str , int k ) { char [ ] s = str . ToCharArray ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp [ i % k ] = s [ i ] ; } } for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! mp . ContainsKey ( i % k ) ) { Console . WriteLine ( - 1 ) ; return ; } s [ i ] = ( char ) mp [ i % k ] ; } Console . WriteLine ( new string ( s ) ) ; } static void Main ( ) { string S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; } }