Sub-string that contains all lowercase alphabets after performing the given operation

• Difficulty Level : Hard
• Last Updated : 10 May, 2021

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 then 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++

 // C++ implementation of the approach#include using namespace std; // Function that returns true if every// lowercase character appears atmost oncebool valid(int cnt[]){    // every character frequency must be not    // greater than one    for (int i = 0; i < 26; i++) {        if (cnt[i] >= 2)            return false;    }     return true;} // Function that returns the modified// good string if possiblestring getGoodString(string s, int n){    // If the length of the string is less than n    if (n < 26)        return "-1";     // Sub-strings of length 26    for (int i = 25; i < n; i++) {         // To store frequency of each character        int cnt = { 0 };         // Get the frequency of each character        // in the current sub-string        for (int j = i; j >= i - 25; j--) {            cnt[s[j] - 'a']++;        }         // Check if we can get sub-string containing all        // the 26 characters        if (valid(cnt)) {             // Find which character is missing            int cur = 0;            while (cnt[cur] > 0)                cur++;             for (int j = i - 25; j <= i; j++) {                 // Fill with missing characters                if (s[j] == '?') {                    s[j] = cur + 'a';                    cur++;                     // Find the next missing character                    while (cnt[cur] > 0)                        cur++;                }            }             // Return the modified good string            return s;        }    }     return "-1";} // Driver codeint main(){    string s = "abcdefghijkl?nopqrstuvwxy?";    int n = s.length();     cout << getGoodString(s, n);     return 0;}

Java

 // Java implementation of the approachimport java.util.*; class GFG{     // Function that returns true if every// lowercase character appears atmost oncestatic boolean valid(int []cnt){    // every character frequency must be not    // greater than one    for (int i = 0; i < 26; i++)    {        if (cnt[i] >= 2)            return false;    }     return true;} // Function that returns the modified// good string if possiblestatic String getGoodString(String ss, int n){    char[] s=ss.toCharArray();         // If the length of the string is less than n    if (n < 26)        return "-1";             // To store frequency of each character        int[] cnt = new int;             // Sub-strings of length 26    for (int i = 25; i < n; i++)    {                   // Get the frequency of each character        // in the current sub-string        for (int j = i; j >= i - 25; j--)        {            if (s[j] != '?')            cnt[((int)s[j] - (int)'a')]++;        }         // Check if we can get sub-string containing all        // the 26 characters        if (valid(cnt))        {             // Find which character is missing            int cur = 0;            while (cnt[cur] > 0)                cur++;             for (int j = i - 25; j <= i; j++)            {                 // Fill with missing characters                if (s[j] == '?')                {                    s[j] = (char)(cur + (int)('a'));                    cur++;                     // Find the next missing character                    while (cnt[cur] > 0)                        cur++;                }            }             // Return the modified good string            return new String(s);        }    }     return "-1";} // Driver codepublic static void main (String[] args){    String s = "abcdefghijkl?nopqrstuvwxy?";    int n = s.length();     System.out.println(getGoodString(s, n));}} // This code is contributed by chandan_jnu

Python3

 # Python3 implementation of the approach # Function that returns true if every# lowercase character appears atmost oncedef valid(cnt):     # Every character frequency must    # be not greater than one    for i in range(0, 26):        if cnt[i] >= 2:            return False     return True # Function that returns the modified# good string if possibledef getGoodString(s, n):     # If the length of the string is    # less than n    if n < 26:        return "-1"     # Sub-strings of length 26    for i in range(25, n):         # To store frequency of each character        cnt =  * 26         # Get the frequency of each character        # in the current sub-string        for j in range(i, i - 26, -1):            if s[j] != '?':                cnt[ord(s[j]) - ord('a')] += 1         # Check if we can get sub-string        # containing the 26 characters all        if valid(cnt):             # Find which character is missing            cur = 0            while cur < 26 and cnt[cur] > 0:                cur += 1             for j in range(i - 25, i + 1):                 # Fill with missing characters                if s[j] == '?':                    s[j] = chr(cur + ord('a'))                    cur += 1                     # Find the next missing character                    while cur < 26 and cnt[cur] > 0:                        cur += 1             # Return the modified good string            return ''.join(s)     return "-1" # Driver codeif __name__ == "__main__":     s = "abcdefghijkl?nopqrstuvwxy?"    n = len(s)     print(getGoodString(list(s), n)) # This code is contributed by Rituraj Jain

C#

 // C# implementation of the approachusing System; class GFG{     // Function that returns true if every// lowercase character appears atmost oncestatic bool valid(int []cnt){    // every character frequency must be not    // greater than one    for (int i = 0; i < 26; i++)    {        if (cnt[i] >= 2)            return false;    }     return true;} // Function that returns the modified// good string if possiblestatic string getGoodString(string ss, int n){    char[] s = ss.ToCharArray();         // If the length of the string is less than n    if (n < 26)        return "-1";             // To store frequency of each character        int[] cnt = new int;             // Sub-strings of length 26    for (int i = 25; i < n; i++)    {                   // Get the frequency of each character        // in the current sub-string        for (int j = i; j >= i - 25; j--)        {            if (s[j] != '?')            cnt[((int)s[j] - (int)'a')]++;        }         // Check if we can get sub-string containing all        // the 26 characters        if (valid(cnt))        {             // Find which character is missing            int cur = 0;            while (cnt[cur] > 0)                cur++;             for (int j = i - 25; j <= i; j++)            {                 // Fill with missing characters                if (s[j] == '?')                {                    s[j] = (char)(cur + (int)('a'));                    cur++;                     // Find the next missing character                    while (cnt[cur] > 0)                        cur++;                }            }             // Return the modified good string            return new String(s);        }    }     return "-1";} // Driver codestatic void Main(){    string s = "abcdefghijkl?nopqrstuvwxy?";    int n = s.Length;     Console.WriteLine(getGoodString(s, n));}} // This code is contributed by chandan_jnu

PHP

 = 2)            return false;    }     return true;} // Function that returns the modified// good string if possiblefunction getGoodString(\$s, \$n){    // If the length of the string is    // less than n    if (\$n < 26)        return "-1";     // Sub-strings of length 26    for (\$i = 25; \$i < \$n; \$i++)    {         // To store frequency of each character        \$cnt = array_fill(0, 26, NULL);         // Get the frequency of each character        // in the current sub-string        for (\$j = \$i; \$j >= \$i - 25; \$j--)        {            if (\$s[\$j] != '?')            \$cnt[ord(\$s[\$j]) - ord('a')]++;        }         // Check if we can get sub-string        // containing all the 26 characters        if (valid(\$cnt))        {             // Find which character is missing            \$cur = 0;            while (\$cur < 26 && \$cnt[\$cur] > 0)                \$cur++;             for (\$j = \$i - 25; \$j <= \$i; \$j++)            {                 // Fill with missing characters                if (\$s[\$j] == '?')                {                    \$s[\$j] = chr(\$cur + ord('a'));                    \$cur++;                     // Find the next missing character                    while (\$cur < 26 && \$cnt[\$cur] > 0)                        \$cur++;                }            }             // Return the modified good string            return \$s;        }    }     return "-1";} // Driver code\$s = "abcdefghijkl?nopqrstuvwxy?";\$n = strlen(\$s);echo getGoodString(\$s, \$n); // This code is contributed by ita_c?>

Javascript


Output:
abcdefghijklmnopqrstuvwxyz

My Personal Notes arrow_drop_up