# Make String repeating after every K characters by replacing characters at missing place

• Last Updated : 25 Apr, 2022

Given an string S and integer K and there are also some missing character i.e. ( _ ), the task is make the string S repeating after every K characters by replacing the appropriate character at missing place i.e. ( _ ). If it is not possible then print -1.

Note: If there are multiple possible strings, then print the smallest lexicographically string.

Examples:

Input: S = ab_bab, K = 2
Output: ababab
Explanation:
Replace _ with ‘a’ and then the string will follow a repeating sequence after every 2 characters.

Input: S = _b_abc_bc, K = 3
Output: abcabcabc

Approach: This problem can be solved by iterating through the string S. Follow the steps below to solve this problem:

• Initialize an array arr[] of size K with NULL characters.
• Iterate in the range [0, K-1] using the variable i:
• Iterate in the range [i, N-1] using the variable j with increment of K:
• If it is not missing character, fill array arr[i] with current character.
• Otherwise, If character is neither matched with an array arr[] or K occurrence pattern, then return -1.
• If the array arr[] having initially NULL values i.e haven’t found any K occurrence pattern, then fill lexicographically the smallest character i.e ‘a‘.
• Initialize an array ans[] of size n with all value ‘a‘.
• Iterate in the range [0, N] using the variable i and update ans[i] as arr[i%K]
• Finally, convert this ans array into string and return this.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the``// above approach``#include``using` `namespace` `std;` `// Creating function findMissingChar having parameter``// n i.e length of the string``// k is repeating occurrence of character``// s is given string``string findMissingChar(``int` `n, ``int` `k, string s){``    ` `    ``// Creating an array arr of size K,``    ``// initially with NULL values.``    ``vector<``char``>arr(k,``'\0'``);``    ` `    ``// Iterate for loop from 0 to k-1.``    ``for``(``int` `i = 0; i < k; i++)``    ``{``        ` `        ``// Iterate for loop from i to n``        ``// with increment of k.``        ``for``(``int` `j = i; j < n; j += k)``        ``{``            ` `            ``// If it is not missing character``        ``// then fill array arr[i]``        ``// with current character.``            ``if``(s[j] != ``'_'``){``                ``if``(arr[i] == ``'\0'``)``                    ``arr[i] = s[j];``                ``else``                ``{``                  ` `                    ``// If character is neither matched``                    ``// with a array or k occurrence pattern``                    ``// return -1 in this case.``                    ``if``(s[j] != arr[i])``                        ``return` `"-1"``;``                ``}``            ``}``        ``}``                           ` `        ``// If the array having initially null values``        ``// i.e haven't found any k occurrence pattern``        ``// then fill lexicographically``        ``// the smallest character i.e 'a'.``        ``if``(arr[i] == ``'\0'``)``            ``arr[i] = ``'a'``;``            ` `    ``}``            ` `    ``// Creating ans array having size n``    ``// and initialize with 'a'.   ``    ``vector<``char``>ans(n,``'a'``);``    ` `    ``// Filling ans array with suitable``    ``// lexicographically smallest character.``    ``for``(``int` `i=0;i

## Python3

 `# Creating function findMissingChar having parameter``# n i.e length of the string``# k is repeating occurrence of character``# s is given string``def` `findMissingChar(n, k, s):``    ` `    ``# Creating an array arr of size K,``    ``# initially with NULL values.``    ``arr ``=` `['']``*``k``    ` `    ``# Iterate for loop from 0 to k-1.``    ``for` `i ``in` `range``(k):``        ` `        ``# Iterate for loop from i to n``        ``# with increment of k.``        ``for` `j ``in` `range``(i, n, k):``            ` `            ``# If it is not missing character``        ``# then fill array arr[i]``        ``# with current character.``            ``if` `s[j] !``=` `'_'``:``                ``if` `arr[i] ``=``=` `'':``                    ``arr[i] ``=` `s[j]``                ``else``:``                    ``# If character is neither matched``                    ``# with a array or k occurrence pattern``                    ``# return -1 in this case.``                    ``if` `s[j] !``=` `arr[i]:``                        ``return` `-``1``                        ` `                        ` `        ``# If the array having initially null values``        ``# i.e haven't found any k occurrence pattern``        ``# then fill lexicographically``        ``# the smallest character i.e 'a'.``        ``if` `arr[i] ``=``=` `'':``            ``arr[i] ``=` `'a'``            ` `            ` `    ``# Creating ans array having size n``    ``# and initialize with 'a'.     ``    ``ans ``=` `[``'a'``]``*``n``    ` `    ``# Filling ans array with suitable``    ``# lexicographically smallest character.``    ``for` `i ``in` `range``(n):``        ``ans[i] ``=` `arr[i ``%` `k]``    ``return` `''.join(ans)` `  ` `# Driver Code   ``s ``=` `'_b_abc_bc'``n ``=` `len``(s)``k ``=` `3``print``(findMissingChar(n, k, s))`

## Javascript

 ``

Output:

`abcabcabc`

Time Complexity: O(N*K)

Auxiliary Space: O(N)

My Personal Notes arrow_drop_up