Related Articles

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

• Last Updated : 09 Sep, 2021

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:

## 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))`
Output:
`abcabcabc`

Time Complexity: O(N*K)

Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up