Given string S of size N consisting of K distinct characters and (N – K) ‘?’s, the task is to replace all ‘?’ with existing characters from the string such that every substring of size K has consisted of unique characters only. If it is not possible to do so, then print “-1”.
Examples:
Input: S = “????abcd”, K = 4
Output: abcdabcd
Explanation:
Replacing the 4 ‘?’s with “abcd” modifies string S to “abcdabcd”, which satisfies the given condition.
Input: S = “?a?b?c”, K = 3
Output: bacbac
Explanation:
Replacing S[0] with ‘b’, S[2] with ‘c’ and S[4] with ‘a’ modifies string S to “bacbac”, which satisfies the given condition.
Approach: The idea is based on the observation that in the final resultant string, each character must appear after exactly K places, like the (K + 1)th character must be the same as 1st, (K + 2)th character must be the same as 2nd, and so on.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void fillString(string s, int k)
{
unordered_map< int , char > mp;
for ( int i = 0; i < s.size(); i++) {
if (s[i] != '?' ) {
mp[i % k] = s[i];
}
}
for ( int i = 0; i < s.size(); i++) {
if (mp.find(i % k) == mp.end()) {
cout << -1;
return ;
}
s[i] = mp[i % k];
}
cout << s;
}
int main()
{
string S = "????abcd" ;
int K = 4;
fillString(S, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void fillString(String str, int k)
{
char s[] = str.toCharArray();
HashMap<Integer, Character> mp = new HashMap<>();
for ( int i = 0 ; i < s.length; i++) {
if (s[i] != '?' ) {
mp.put(i % k, s[i]);
}
}
for ( int i = 0 ; i < s.length; i++) {
if (!mp.containsKey(i % k)) {
System.out.println(- 1 );
return ;
}
s[i] = mp.getOrDefault(i % k, s[i]);
}
System.out.println( new String(s));
}
public static void main(String[] args)
{
String S = "????abcd" ;
int K = 4 ;
fillString(S, K);
}
}
|
Python3
def fillString(s, k):
mp = {}
for i in range ( len (s)):
if (s[i] ! = '?' ):
mp[i % k] = s[i]
s = list (s)
for i in range ( len (s)):
if ((i % k) not in mp):
print ( - 1 )
return
s[i] = mp[i % k]
s = ''.join(s)
print (s)
if __name__ = = '__main__' :
S = "????abcd"
K = 4
fillString(S, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void fillString( string str, int k)
{
char [] s = str.ToCharArray();
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < s.Length; i++) {
if (s[i] != '?' ) {
mp[i % k] = s[i];
}
}
for ( int i = 0; i < s.Length; i++) {
if (!mp.ContainsKey(i % k)) {
Console.WriteLine(-1);
return ;
}
s[i] = ( char )mp[i % k];
}
Console.WriteLine( new string (s));
}
static void Main()
{
string S = "????abcd" ;
int K = 4;
fillString(S, K);
}
}
|
Javascript
<script>
function fillString(str, k) {
var s = str.split( "" );
var mp = {};
for ( var i = 0; i < s.length; i++) {
if (s[i] !== "?" ) {
mp[i % k] = s[i];
}
}
for ( var i = 0; i < s.length; i++) {
if (!mp.hasOwnProperty(i % k)) {
document.write(-1);
return ;
}
s[i] = mp[i % k];
}
document.write(s.join( "" ) + "<br>" );
}
var S = "????abcd" ;
var K = 4;
fillString(S, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 May, 2021
Like Article
Save Article