Sub-string that contains all lowercase alphabets after performing the given operation
Last Updated :
16 Oct, 2023
Given a string str containing lower case alphabets and character ‘?’. The task is to check if it is possible to make str good or not.
A string is called good if it contains a sub-string of length 26 which has every character of lower case alphabets in it.
The task is to check if it is possible to make the string good by replacing ‘?’ characters with any lower case alphabet. If it is possible then print the modified string otherwise print -1.
Examples:
Input: str = “abcdefghijkl?nopqrstuvwxy?”
Output: abcdefghijklmnopqrstuvwxyz
Replace first ‘?’ with ‘m’ and second with ‘z’.
Input: str = “abcdefghijklmnopqrstuvwxyz??????”
Output: abcdefghijklmnopqrstuvwxyzaaaaaa
Given string already has a sub-string which contains all the 26 lower case alphabets.
Approach:
If the length of the string is less than 26 then print -1. The Task is to make a sub-string of length 26 that has all the lowercase characters. Thus, the simplest way is to iterate through all sub-strings of length 26 then for each sub-string count the number of occurrences of each alphabet, ignoring the question marks. After that, if there exists a character that occurs twice or more than this sub-string cannot contain all letters of the alphabet, and we process the next sub-string. Otherwise, we can fill in the question marks with the letters that have not appeared in the sub-string and obtain a sub-string of length 26 which contains all letters of the alphabet.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool valid( int cnt[])
{
for ( int i = 0; i < 26; i++) {
if (cnt[i] >= 2)
return false ;
}
return true ;
}
string getGoodString(string s, int n)
{
if (n < 26)
return "-1" ;
for ( int i = 25; i < n; i++) {
int cnt[26] = { 0 };
for ( int j = i; j >= i - 25; j--) {
cnt[s[j] - 'a' ]++;
}
if (valid(cnt)) {
int cur = 0;
while (cnt[cur] > 0)
cur++;
for ( int j = i - 25; j <= i; j++) {
if (s[j] == '?' ) {
s[j] = cur + 'a' ;
cur++;
while (cnt[cur] > 0)
cur++;
}
}
return s;
}
}
return "-1" ;
}
int main()
{
string s = "abcdefghijkl?nopqrstuvwxy?" ;
int n = s.length();
cout << getGoodString(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean valid( int []cnt)
{
for ( int i = 0 ; i < 26 ; i++)
{
if (cnt[i] >= 2 )
return false ;
}
return true ;
}
static String getGoodString(String ss, int n)
{
char [] s=ss.toCharArray();
if (n < 26 )
return "-1" ;
int [] cnt = new int [ 27 ];
for ( int i = 25 ; i < n; i++)
{
for ( int j = i; j >= i - 25 ; j--)
{
if (s[j] != '?' )
cnt[(( int )s[j] - ( int ) 'a' )]++;
}
if (valid(cnt))
{
int cur = 0 ;
while (cnt[cur] > 0 )
cur++;
for ( int j = i - 25 ; j <= i; j++)
{
if (s[j] == '?' )
{
s[j] = ( char )(cur + ( int )( 'a' ));
cur++;
while (cnt[cur] > 0 )
cur++;
}
}
return new String(s);
}
}
return "-1" ;
}
public static void main (String[] args)
{
String s = "abcdefghijkl?nopqrstuvwxy?" ;
int n = s.length();
System.out.println(getGoodString(s, n));
}
}
|
Python3
def valid(cnt):
for i in range ( 0 , 26 ):
if cnt[i] > = 2 :
return False
return True
def getGoodString(s, n):
if n < 26 :
return "-1"
for i in range ( 25 , n):
cnt = [ 0 ] * 26
for j in range (i, i - 26 , - 1 ):
if s[j] ! = '?' :
cnt[ ord (s[j]) - ord ( 'a' )] + = 1
if valid(cnt):
cur = 0
while cur < 26 and cnt[cur] > 0 :
cur + = 1
for j in range (i - 25 , i + 1 ):
if s[j] = = '?' :
s[j] = chr (cur + ord ( 'a' ))
cur + = 1
while cur < 26 and cnt[cur] > 0 :
cur + = 1
return ''.join(s)
return "-1"
if __name__ = = "__main__" :
s = "abcdefghijkl?nopqrstuvwxy?"
n = len (s)
print (getGoodString( list (s), n))
|
C#
using System;
class GFG
{
static bool valid( int []cnt)
{
for ( int i = 0; i < 26; i++)
{
if (cnt[i] >= 2)
return false ;
}
return true ;
}
static string getGoodString( string ss, int n)
{
char [] s = ss.ToCharArray();
if (n < 26)
return "-1" ;
int [] cnt = new int [27];
for ( int i = 25; i < n; i++)
{
for ( int j = i; j >= i - 25; j--)
{
if (s[j] != '?' )
cnt[(( int )s[j] - ( int ) 'a' )]++;
}
if (valid(cnt))
{
int cur = 0;
while (cnt[cur] > 0)
cur++;
for ( int j = i - 25; j <= i; j++)
{
if (s[j] == '?' )
{
s[j] = ( char )(cur + ( int )( 'a' ));
cur++;
while (cnt[cur] > 0)
cur++;
}
}
return new String(s);
}
}
return "-1" ;
}
static void Main()
{
string s = "abcdefghijkl?nopqrstuvwxy?" ;
int n = s.Length;
Console.WriteLine(getGoodString(s, n));
}
}
|
Javascript
<script>
function valid(cnt) {
for ( var i = 0; i < 26; i++) {
if (cnt[i] >= 2) return false ;
}
return true ;
}
function getGoodString(ss, n) {
var s = ss.split( "" );
if (n < 26) return "-1" ;
for ( var i = 25; i < n; i++) {
var cnt = new Array(26).fill(0);
for ( var j = i; j >= i - 25; j--) {
cnt[s[j].charCodeAt(0) - "a" .charCodeAt(0)]++;
}
if (valid(cnt)) {
var cur = 0;
while (cnt[cur] > 0) cur++;
for ( var j = i - 25; j <= i; j++) {
if (s[j] === "?" ) {
s[j] = String.fromCharCode(cur + "a" .charCodeAt(0));
cur++;
while (cnt[cur] > 0) cur++;
}
}
return s.join( "" );
}
}
return "-1" ;
}
var s = "abcdefghijkl?nopqrstuvwxy?" ;
var n = s.length;
document.write(getGoodString(s, n));
</script>
|
PHP
<?php
function valid(& $cnt )
{
for ( $i = 0; $i < 26; $i ++)
{
if ( $cnt [ $i ] >= 2)
return false;
}
return true;
}
function getGoodString( $s , $n )
{
if ( $n < 26)
return "-1" ;
for ( $i = 25; $i < $n ; $i ++)
{
$cnt = array_fill (0, 26, NULL);
for ( $j = $i ; $j >= $i - 25; $j --)
{
if ( $s [ $j ] != '?' )
$cnt [ord( $s [ $j ]) - ord( 'a' )]++;
}
if (valid( $cnt ))
{
$cur = 0;
while ( $cur < 26 && $cnt [ $cur ] > 0)
$cur ++;
for ( $j = $i - 25; $j <= $i ; $j ++)
{
if ( $s [ $j ] == '?' )
{
$s [ $j ] = chr ( $cur + ord( 'a' ));
$cur ++;
while ( $cur < 26 && $cnt [ $cur ] > 0)
$cur ++;
}
}
return $s ;
}
}
return "-1" ;
}
$s = "abcdefghijkl?nopqrstuvwxy?" ;
$n = strlen ( $s );
echo getGoodString( $s , $n );
?>
|
Output
abcdefghijklmnopqrstuvwxyz
Complexity Analysis:
- Time Complexity: O(N2), where N is the size of the given string.
- Auxiliary Space: O(1)
Approach – 2 :
Here is an alternate approach to the problem:
Algorithm:
- Traverse the string from left to right.
- For each position i in the string, check if there exists a substring of length 26 starting from position i that contains all lowercase English alphabets.
- If such a substring is found, replace all the ‘?’ characters in the substring with the missing characters such that each lowercase English alphabet appears exactly once in the substring.
- If no such substring is found, return -1.
Code:
C++
#include <bits/stdc++.h>
using namespace std;
string getGoodString(string s) {
int n = s.size();
vector< int > freq(26, 0);
for ( int i = 0; i < n - 25; i++) {
bool found = true ;
fill(freq.begin(), freq.end(), 0);
for ( int j = i; j < i + 26; j++) {
if (s[j] != '?' ) {
freq[s[j] - 'a' ]++;
if (freq[s[j] - 'a' ] > 1) {
found = false ;
break ;
}
}
}
if (found) {
int idx = 0;
for ( int j = i; j < i + 26; j++) {
if (s[j] == '?' ) {
while (freq[idx]) {
idx++;
}
s[j] = idx + 'a' ;
freq[idx] = 1;
}
}
for ( int j = 0; j < n; j++) {
if (s[j] == '?' ) {
s[j] = 'a' ;
}
}
return s;
}
}
return "-1" ;
}
int main() {
string s = "abcdefghijkl?nopqrstuvwxy?" ;
cout << getGoodString(s) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GoodString {
public static String getGoodString(String s) {
int n = s.length();
int [] freq = new int [ 26 ];
for ( int i = 0 ; i < n - 25 ; i++) {
boolean found = true ;
Arrays.fill(freq, 0 );
for ( int j = i; j < i + 26 ; j++) {
if (s.charAt(j) != '?' ) {
freq[s.charAt(j) - 'a' ]++;
if (freq[s.charAt(j) - 'a' ] > 1 ) {
found = false ;
break ;
}
}
}
if (found) {
int idx = 0 ;
for ( int j = i; j < i + 26 ; j++) {
if (s.charAt(j) == '?' ) {
while (freq[idx] > 0 ) {
idx++;
}
s = s.substring( 0 , j) + ( char ) (idx + 'a' ) + s.substring(j + 1 );
freq[idx] = 1 ;
}
}
for ( int j = 0 ; j < n; j++) {
if (s.charAt(j) == '?' ) {
s = s.substring( 0 , j) + 'a' + s.substring(j + 1 );
}
}
return s;
}
}
return "-1" ;
}
public static void main(String[] args) {
String s = "abcdefghijkl?nopqrstuvwxy?" ;
System.out.println(getGoodString(s));
}
}
|
Python3
def getGoodString(s):
n = len (s)
freq = [ 0 ] * 26
for i in range (n - 25 ):
found = True
freq = [ 0 ] * 26
for j in range (i, i + 26 ):
if s[j] ! = '?' :
freq[ ord (s[j]) - ord ( 'a' )] + = 1
if freq[ ord (s[j]) - ord ( 'a' )] > 1 :
found = False
break
if found:
idx = 0
for j in range (i, i + 26 ):
if s[j] = = '?' :
while freq[idx]:
idx + = 1
s = s[:j] + chr (idx + ord ( 'a' )) + s[j + 1 :]
freq[idx] = 1
for j in range (n):
if s[j] = = '?' :
s = s[:j] + 'a' + s[j + 1 :]
return s
return "-1"
def main():
s = "abcdefghijkl?nopqrstuvwxy?"
print (getGoodString(s))
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static string GetGoodString( string s)
{
int n = s.Length;
int [] freq = new int [26];
for ( int i = 0; i <= n - 26; i++)
{
bool found = true ;
Array.Clear(freq, 0, freq.Length);
for ( int j = i; j < i + 26; j++)
{
if (s[j] != '?' )
{
freq[s[j] - 'a' ]++;
if (freq[s[j] - 'a' ] > 1)
{
found = false ;
break ;
}
}
}
if (found)
{
int idx = 0;
for ( int j = i; j < i + 26; j++)
{
if (s[j] == '?' )
{
while (freq[idx] != 0)
{
idx++;
}
s = s.Substring(0, j) + ( char )(idx + 'a' ) + s.Substring(j + 1);
freq[idx] = 1;
}
}
for ( int j = 0; j < n; j++)
{
if (s[j] == '?' )
{
s = s.Substring(0, j) + 'a' + s.Substring(j + 1);
}
}
return s;
}
}
return "-1" ;
}
static void Main()
{
string s = "abcdefghijkl?nopqrstuvwxy?" ;
Console.WriteLine(GetGoodString(s));
}
}
|
Javascript
function getGoodString(s) {
const n = s.length;
const freq = new Array(26).fill(0);
for (let i = 0; i < n - 25; i++) {
let found = true ;
freq.fill(0);
for (let j = i; j < i + 26; j++) {
if (s[j] !== '?' ) {
freq[s[j].charCodeAt() - 97]++;
if (freq[s[j].charCodeAt() - 97] > 1) {
found = false ;
break ;
}
}
}
if (found) {
let idx = 0;
for (let j = i; j < i + 26; j++) {
if (s[j] === '?' ) {
while (freq[idx]) {
idx++;
}
s = s.substring(0, j) + String.fromCharCode(idx + 97) + s.substring(j + 1);
freq[idx] = 1;
}
}
for (let j = 0; j < n; j++) {
if (s[j] === '?' ) {
s = s.substring(0, j) + 'a' + s.substring(j + 1);
}
}
return s;
}
}
return "-1" ;
}
const s = "abcdefghijkl?nopqrstuvwxy?" ;
console.log(getGoodString(s));
|
Output
abcdefghijklmnopqrstuvwxyz
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...