Make String repeating after every K characters by replacing characters at missing place
Last Updated :
27 Feb, 2023
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.
Java
import java.util.Arrays;
public class Main {
public static String findMissingChar(int n, int k,
String s)
{
char[] arr = new char[k];
Arrays.fill(arr, '\0');
for (int i = 0; i < k; i++) {
for (int j = i; j < n; j += k) {
if (s.charAt(j) != '_') {
if (arr[i] == '\0')
arr[i] = s.charAt(j);
else {
if (s.charAt(j) != arr[i])
return "-1";
}
}
}
if (arr[i] == '\0')
arr[i] = 'a';
}
char[] ans = new char[n];
Arrays.fill(ans, 'a');
for (int i = 0; i < n; i++) {
ans[i] = arr[i % k];
}
return new String(ans);
}
public static void main(String[] args)
{
String s = "_b_abc_bc";
int n = s.length();
int k = 3;
System.out.println(findMissingChar(n, k, s));
}
}
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>
C#
using System;
using System.Collections.Generic;
public class Program {
// Creating function findMissingChar having parameter
// n i.e length of the string
// k is repeating occurrence of character
// s is given string
static string FindMissingChar(int n, int k, string s)
{
// Creating an array arr of size K,
// initially with NULL values.
var arr = new char[k];
for (int i = 0; i < k; i++) {
arr[i] = '\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'.
var ans = new char[n];
for (int i = 0; i < n; i++) {
ans[i] = 'a';
}
// Filling ans array with suitable
// lexicographically smallest character.
for (int i = 0; i < n; i++) {
ans[i] = arr[i % k];
}
return new string(ans);
}
// Driver Code
public static void Main()
{
string s = "_b_abc_bc";
int n = s.Length;
int k = 3;
Console.WriteLine(FindMissingChar(n, k, s));
}
}
Time Complexity: O(N*K)
Auxiliary Space: O(N)