📌  相关文章
📜  执行给定操作后包含所有小写字母的子字符串

📅  最后修改于: 2021-04-29 04:36:54             🧑  作者: Mango

给定一个包含小写字母和字符‘?’的字符串str 。任务是检查是否有可能使str变

如果字符串包含长度为26的子字符串,且其中包含小写字母的每个字符,则该字符串称为

任务是检查是否可以通过替换‘?’来使字符串好与任何小写字母字符。如果可能,则打印修改后的字符串,否则打印-1

例子:

方法:如果字符串的长度小于26,则打印-1 。任务是制作一个长度为26的子字符串,其中包含所有小写字符。因此,最简单的方法是遍历所有长度为26的子字符串,然后对每个子字符串计数每个字母的出现次数,而忽略问号。此后,如果存在一个出现两次或两次以上的字符,则此子字符串不能包含字母表中的所有字母,因此我们将处理下一个子字符串。否则,我们可以用未出现在子字符串中的字母填充问号,并获得长度为26的子字符串,其中包含字母表中的所有字母。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if every
// lowercase character appears atmost once
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 possible
string 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[26] = { 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 code
int main()
{
    string s = "abcdefghijkl?nopqrstuvwxy?";
    int n = s.length();
  
    cout << getGoodString(s, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
// Function that returns true if every
// lowercase character appears atmost once
static 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 possible
static 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[27];
          
    // 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 code
public 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 once 
def 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 possible 
def 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 = [0] * 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 code 
if __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 approach
using System;
  
class GFG
{
      
// Function that returns true if every
// lowercase character appears atmost once
static 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 possible
static 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[27];
          
    // 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 code
static 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 possible
function 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
?>


输出:
abcdefghijklmnopqrstuvwxyz