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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fillString ( string s , int k ) { unordered_map < int , char > mp ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] != ' ? ' ) { mp [ i % k ] = s [ i ] ; } } for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( mp . find ( i % k ) == mp . end ( ) ) { cout << -1 ; return ; } s [ i ] = mp [ i % k ] ; } cout << s ; } int main ( ) { string S = " ? ? ? ? abcd " ; int K = 4 ; fillString ( S , K ) ; return 0 ; }