# Modify string by inserting characters such that every K-length substring consists of unique characters only

• Difficulty Level : Expert
• Last Updated : 27 May, 2021

Given string S of size N consisting of K distinct characters and (N – K) ‘?’s, the task is to replace all ‘?’ with existing characters from the string such that every substring of size K has consisted of unique characters only. If it is not possible to do so, then print “-1”.

Examples:

Input: S = “????abcd”, K = 4
Output: abcdabcd
Explanation:
Replacing the 4 ‘?’s with “abcd” modifies string S to “abcdabcd”, which satisfies the given condition.

Input: S = “?a?b?c”, K = 3
Output: bacbac
Explanation:
Replacing S with ‘b’, S with ‘c’ and S with ‘a’ modifies string S to “bacbac”, which satisfies the given condition.

Approach: The idea is based on the observation that in the final resultant string, each character must appear after exactly K places, like the (K + 1)th character must be the same as 1st, (K + 2)th character must be the same as 2nd, and so on.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to replace all '?'``// characters in a string such``// that the given conditions are satisfied``void` `fillString(string s, ``int` `k)``{``    ``unordered_map<``int``, ``char``> mp;` `    ``// Traverse the string to Map the``    ``// characters with respective positions``    ``for` `(``int` `i = 0; i < s.size(); i++) {``        ``if` `(s[i] != ``'?'``) {``            ``mp[i % k] = s[i];``        ``}``    ``}` `    ``// Traverse the string again and``    ``// replace all unknown characters``    ``for` `(``int` `i = 0; i < s.size(); i++) {` `        ``// If i % k is not found in``        ``// the Map M, then return -1``        ``if` `(mp.find(i % k) == mp.end()) {` `            ``cout << -1;``            ``return``;``        ``}` `        ``// Update S[i]``        ``s[i] = mp[i % k];``    ``}` `    ``// Print the string S``    ``cout << s;``}` `// Driver Code``int` `main()``{``    ``string S = ``"????abcd"``;``    ``int` `K = 4;``    ``fillString(S, K);` `    ``return` `0;``}`

## Java

 `// Java Program to implement``// the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``// Function to replace all '?'``    ``// characters in a string such``    ``// that the given conditions are satisfied``    ``static` `void` `fillString(String str, ``int` `k)``    ``{` `        ``char` `s[] = str.toCharArray();` `        ``HashMap mp = ``new` `HashMap<>();` `        ``// Traverse the string to Map the``        ``// characters with respective positions``        ``for` `(``int` `i = ``0``; i < s.length; i++) {``            ``if` `(s[i] != ``'?'``) {``                ``mp.put(i % k, s[i]);``            ``}``        ``}` `        ``// Traverse the string again and``        ``// replace all unknown characters``        ``for` `(``int` `i = ``0``; i < s.length; i++) {` `            ``// If i % k is not found in``            ``// the Map M, then return -1``            ``if` `(!mp.containsKey(i % k)) {``                ``System.out.println(-``1``);``                ``return``;``            ``}` `            ``// Update S[i]``            ``s[i] = mp.getOrDefault(i % k, s[i]);``        ``}` `        ``// Print the string S``        ``System.out.println(``new` `String(s));``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``String S = ``"????abcd"``;``        ``int` `K = ``4``;``        ``fillString(S, K);``    ``}``}` `// This code is contributed by Kingash.`

## Python3

 `# Python 3 program for the above approach` `# Function to replace all '?'``# characters in a string such``# that the given conditions are satisfied``def` `fillString(s, k):``    ``mp ``=` `{}` `    ``# Traverse the string to Map the``    ``# characters with respective positions``    ``for` `i ``in` `range``(``len``(s)):``        ``if` `(s[i] !``=` `'?'``):``            ``mp[i ``%` `k] ``=` `s[i]` `    ``# Traverse the string again and``    ``# replace all unknown characters``    ``s ``=` `list``(s)``    ``for` `i ``in` `range``(``len``(s)):``      ` `        ``# If i % k is not found in``        ``# the Map M, then return -1``        ``if` `((i ``%` `k) ``not` `in` `mp):``            ``print``(``-``1``)``            ``return` `        ``# Update S[i]``        ``s[i] ``=` `mp[i ``%` `k]` `    ``# Print the string S``    ``s ``=`   `''.join(s)``    ``print``(s)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``S ``=` `"????abcd"``    ``K ``=` `4``    ``fillString(S, K)` `    ``# This code is contributed by bgangwar59.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `  ``// Function to replace all '?'``  ``// characters in a string such``  ``// that the given conditions are satisfied``  ``static` `void` `fillString(``string` `str, ``int` `k)``  ``{` `    ``char``[] s = str.ToCharArray();` `    ``Dictionary<``int``,``    ``int``> mp = ``new` `Dictionary<``int``,``    ``int``>();` `    ``// Traverse the string to Map the``    ``// characters with respective positions``    ``for` `(``int` `i = 0; i < s.Length; i++) {``      ``if` `(s[i] != ``'?'``) {``        ``mp[i % k] = s[i];``      ``}``    ``}` `    ``// Traverse the string again and``    ``// replace all unknown characters``    ``for` `(``int` `i = 0; i < s.Length; i++) {` `      ``// If i % k is not found in``      ``// the Map M, then return -1``      ``if` `(!mp.ContainsKey(i % k)) {``        ``Console.WriteLine(-1);``        ``return``;``      ``}` `      ``// Update S[i]``      ``s[i] = (``char``)mp[i % k];` `    ``}` `    ``// Print the string S``    ``Console.WriteLine(``new` `string``(s));``  ``}` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``string` `S = ``"????abcd"``;``    ``int` `K = 4;``    ``fillString(S, K);``  ``}``}` `// This code is contributed by susmitakundugoaldanga.`

## Javascript

 ``
Output:
`abcdabcd`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up