# Lexicographically smallest string with period K possible by replacing ‘?’s from a given string

Given a string **S** consisting of **N** lowercase characters and character **‘?’** and a positive integer **K**, the task is to replace each character **‘?’** with some lowercase alphabets such that the given string becomes a period of **K**. If it is not possible to do so, then print **“-1”**.

A string is said to be a period of

Kif and only if the length of the string is a multiple ofKand for all possible value ofiover the range[0, K)the value S[i + K], S[i + 2*K], S[i + 3*K], …, remains the same.

**Examples:**

Input:S = “ab??”, K = 2Output:abab

Input:S = “??????”, K = 3Output:aaaaaa

**Naive Approach:** The given approach can also be solved by generating all possible combination of strings by replacing each character **‘?’** with any lowercase characters and print that string that have each substring of **size K** is the same.

**Time Complexity:** O(26^{M}), where **M** is the number of **‘?’** in the string **S**.**Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can also be optimized by traversing the string in a way such that the first, second, third, and so on characters are traversed and if all the characters are ‘?’ then replace it with character **‘a’**, Otherwise, if there exists only one distinct character at each respective position then replace **‘?’** with that character, Otherwise, the string can’t be modified as per the given criteria and hence, print “-1”. Follow the steps below to solve the problem:

- Iterate a loop over the range
**[0, K]**using the variable**i**and perform the following steps:- Initialize a map, say
**M**to store the frequency of characters of substring at positions**i**. - Traverse the given string over the range
**[i, N]**using the variable**j**with an increment of**K**and store the frequency of the character**S[j]**by**1**in the map**M**. - After completing the above steps perform the following:
- If the size of the map is greater than
**2**, then print “-1” and break out of the loop. - Otherwise, if the size of the map is
**2**, then replace each**‘?’**with that different character. - Otherwise, replace all the
**‘?’**with the character**‘a’**.

- If the size of the map is greater than

- Initialize a map, say
- After completing the above steps, if the string can be modified, then print the string
**S**as the result string.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include "bits/stdc++.h"` `using` `namespace` `std;` `// Function to modify the given string` `// such that string S is a period of K` `string modifyString(string& S, ` `int` `K)` `{` ` ` `int` `N = S.length();` ` ` `// Iterate over the range [0, K]` ` ` `for` `(` `int` `i = 0; i < K; i++) {` ` ` `// Stores the frequency of the` ` ` `// characters S[i + j*K]` ` ` `map<` `char` `, ` `int` `> M;` ` ` `// Iterate over the string with` ` ` `// increment of K` ` ` `for` `(` `int` `j = i; j < N; j += K) {` ` ` `M[S[j]]++;` ` ` `}` ` ` `// Print "-1"` ` ` `if` `(M.size() > 2) {` ` ` `return` `"-1"` `;` ` ` `}` ` ` `else` `if` `(M.size() == 1) {` ` ` `if` `(M[` `'?'` `] != 0) {` ` ` `// Replace all characters` ` ` `// of the string with '?'` ` ` `// to 'a' to make it smallest` ` ` `for` `(` `int` `j = i; j < N; j += K) {` ` ` `S[j] = ` `'a'` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Otherwise` ` ` `else` `if` `(M.size() == 2) {` ` ` `char` `ch;` ` ` `// Find the character other` ` ` `// than '?'` ` ` `for` `(` `auto` `& it : M) {` ` ` `if` `(it.first != ` `'?'` `) {` ` ` `ch = it.first;` ` ` `}` ` ` `}` ` ` `// Replace all characters` ` ` `// of the string with '?'` ` ` `// to character ch` ` ` `for` `(` `int` `j = i; j < N; j += K) {` ` ` `S[j] = ch;` ` ` `}` ` ` `}` ` ` `// Clear the map M` ` ` `M.clear();` ` ` `}` ` ` `// Return the modified string` ` ` `return` `S;` `}` `// Driver Code` `int` `main()` `{` ` ` `string S = ` `"ab??"` `;` ` ` `int` `K = 2;` ` ` `cout << modifyString(S, K);` ` ` `return` `0;` `}` |

## Javascript

`<script>` `// JavaScript program for the above approach` `// Function to modify the given string` `// such that string S is a period of K` `function` `modifyString(S, K)` `{` ` ` `let N = S.length;` ` ` `// Iterate over the range [0, K]` ` ` `for` `(let i = 0; i < K; i++)` ` ` `{` ` ` ` ` `// Stores the frequency of the` ` ` `// characters S[i + j*K]` ` ` `let M = ` `new` `Map();` ` ` `// Iterate over the string with` ` ` `// increment of K` ` ` `for` `(let j = i; j < N; j += K)` ` ` `{` ` ` `if` `(M.has(S[j]))` ` ` `{` ` ` `M.set(M.get(S[j]),` ` ` `M.get(S[j]) + 1);` ` ` `}` ` ` `else` ` ` `{` ` ` `M.set(S[j], 1);` ` ` `}` ` ` `}` ` ` `// Print "-1"` ` ` `if` `(M.size > 2)` ` ` `{` ` ` `return` `"-1"` `;` ` ` `}` ` ` `else` `if` `(M.size == 1)` ` ` `{` ` ` `if` `(M.has(` `'?'` `))` ` ` `{` ` ` ` ` `// Replace all characters` ` ` `// of the string with '?'` ` ` `// to 'a' to make it smallest` ` ` `for` `(let j = i; j < N; j += K)` ` ` `{` ` ` `S = S.replace(S[j], ` `'a'` `);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Otherwise` ` ` `else` `if` `(M.size == 2)` ` ` `{` ` ` `let ch;` ` ` `// Find the character other` ` ` `// than '?'` ` ` `for` `(let it of M.keys())` ` ` `{` ` ` `if` `(it != ` `'?'` `)` ` ` `{` ` ` `ch = it;` ` ` `}` ` ` `}` ` ` `// Replace all characters` ` ` `// of the string with '?'` ` ` `// to character ch` ` ` `for` `(let j = i; j < N; j += K)` ` ` `{` ` ` `S = S.replace(S[j], ch);` ` ` `}` ` ` `}` ` ` ` ` `// Clear the map M` ` ` `M.clear();` ` ` `}` ` ` `// Return the modified string` ` ` `return` `S;` `}` `// Driver Code` `let S = ` `"ab??"` `;` `let K = 2;` `document.write(modifyString(S, K));` `// This code is contributed by Potta Lokesh` `</script>` |

## Python3

`# python 3 program for the above approach` `# Function to modify the given string` `# such that string S is a period of K` `def` `modifyString(S,K):` ` ` `N ` `=` `len` `(S)` ` ` `S ` `=` `list` `(S)` ` ` `# Iterate over the range [0, K]` ` ` `for` `i ` `in` `range` `(K):` ` ` `# Stores the frequency of the` ` ` `# characters S[i + j*K]` ` ` `M ` `=` `{}` ` ` `# Iterate over the string with` ` ` `# increment of K` ` ` `for` `j ` `in` `range` `(i,N,K):` ` ` `if` `S[j] ` `in` `M:` ` ` `M[S[j]] ` `+` `=` `1` ` ` `else` `:` ` ` `M[S[j]] ` `=` `1` ` ` `# Print "-1"` ` ` `if` `(` `len` `(M) > ` `2` `):` ` ` `return` `"-1"` ` ` `elif` `(` `len` `(M) ` `=` `=` `1` `):` ` ` `if` `(M[` `'?'` `] !` `=` `0` `):` ` ` `# Replace all characters` ` ` `# of the string with '?'` ` ` `# to 'a' to make it smallest` ` ` `for` `j ` `in` `range` `(i,N,K):` ` ` `S[j] ` `=` `'a'` ` ` `# Otherwise` ` ` `elif` `(` `len` `(M) ` `=` `=` `2` `):` ` ` `ch ` `=` `''` ` ` `# Find the character other` ` ` `# than '?'` ` ` `for` `key,value ` `in` `M.items():` ` ` `if` `(key !` `=` `'?'` `):` ` ` `ch ` `=` `key` ` ` `# Replace all characters` ` ` `# of the string with '?'` ` ` `# to character ch` ` ` `for` `j ` `in` `range` `(i,N,K):` ` ` `S[j] ` `=` `ch` ` ` `# Clear the map M` ` ` `M.clear()` ` ` `S ` `=` `''.join(S)` ` ` `# Return the modified string` ` ` `return` `S` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `S ` `=` `"ab??"` ` ` `K ` `=` `2` ` ` `print` `(modifyString(S, K))` ` ` ` ` `# This code is contributed by ipg2016107.` |

**Output:**

abab

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.