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++
#include<bits/stdc++.h>
using namespace std;
string findMissingChar( int n, int k, string s){
vector< char >arr(k, '\0' );
for ( int i = 0; i < k; i++)
{
for ( int j = i; j < n; j += k)
{
if (s[j] != '_' ){
if (arr[i] == '\0' )
arr[i] = s[j];
else
{
if (s[j] != arr[i])
return "-1" ;
}
}
}
if (arr[i] == '\0' )
arr[i] = 'a' ;
}
vector< char >ans(n, 'a' );
for ( int i=0;i<n;i++){
ans[i] = arr[i % k];
}
string res(ans.begin(),ans.end());
return res;
}
int main(){
string s = "_b_abc_bc" ;
int n = s.length();
int k = 3;
cout<<findMissingChar(n, k, s)<<endl;
}
|
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
def findMissingChar(n, k, s):
arr = [''] * k
for i in range (k):
for j in range (i, n, k):
if s[j] ! = '_' :
if arr[i] = = '':
arr[i] = s[j]
else :
if s[j] ! = arr[i]:
return - 1
if arr[i] = = '':
arr[i] = 'a'
ans = [ 'a' ] * n
for i in range (n):
ans[i] = arr[i % k]
return ''.join(ans)
s = '_b_abc_bc'
n = len (s)
k = 3
print (findMissingChar(n, k, s))
|
Javascript
<script>
function findMissingChar(n, k, s){
let arr = new Array(k).fill( '' )
for (let i=0;i<k;i++){
for (let j=i;j<n;j+=k){
if (s[j] != '_' ){
if (arr[i] == '' )
arr[i] = s[j]
else {
if (s[j] != arr[i])
return -1
}
}
}
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>" )
</script>
|
C#
using System;
using System.Collections.Generic;
public class Program {
static string FindMissingChar( int n, int k, string s)
{
var arr = new char [k];
for ( int i = 0; i < k; i++) {
arr[i] = '\0' ;
}
for ( int i = 0; i < k; i++) {
for ( int j = i; j < n; j += k) {
if (s[j] != '_' ) {
if (arr[i] == '\0' ) {
arr[i] = s[j];
}
else {
if (s[j] != arr[i]) {
return "-1" ;
}
}
}
}
if (arr[i] == '\0' ) {
arr[i] = 'a' ;
}
}
var ans = new char [n];
for ( int i = 0; i < n; i++) {
ans[i] = 'a' ;
}
for ( int i = 0; i < n; i++) {
ans[i] = arr[i % k];
}
return new string (ans);
}
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)
Share your thoughts in the comments
Please Login to comment...