Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Apr, 2022
View Discussion
Improve Article
Save Article

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<bits/stdc++.h>
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<n;i++){
        ans[i] = arr[i % k];
    }
 
    string res(ans.begin(),ans.end());
 
    return res;
 
}
 
// Driver Code
int main(){
    string s = "_b_abc_bc";
    int n = s.length();
    int k = 3;
    cout<<findMissingChar(n, k, s)<<endl;
}
 
// This code is contributed by shinjanpatra.

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




<script>
 
// Creating function findMissingChar having parameter
// n i.e length of the string
// k is repeating occurrence of character
// s is given string
function findMissingChar(n, k, s){
     
    // Creating an array arr of size K,
    // initially with NULL values.
    let arr = new Array(k).fill('')
     
    // Iterate for loop from 0 to k-1.
    for(let i=0;i<k;i++){
         
        // Iterate for loop from i to n
        // with increment of k.
        for(let 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] == '')
                    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'.   
    let ans = new Array(n).fill('a');
     
    // Filling ans array with suitable
    // lexicographically smallest character.
    for(let i=0;i<n;i++){
        ans[i] = arr[i % k]
    }
 
    return ans.join('');
 
}
 
// Driver Code
let s = '_b_abc_bc'
let n = s.length
let k = 3
document.write(findMissingChar(n, k, s),"</br>")
 
// This code is contributed by shinjanpatra.
</script>

Output: 

abcabcabc

 

Time Complexity: O(N*K)

Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!