📜  检查字符串是否可以在 K 次更改中转换为 Pangram

📅  最后修改于: 2022-05-13 01:57:06.588000             🧑  作者: Mango

检查字符串是否可以在 K 次更改中转换为 Pangram

给定一个仅包含小写英文字母和整数K的字符串str 。任务是通过执行最多 K 次更改来检查字符串是否可以转换为 Pangram。在一项更改中,我们可以删除任何现有字符并添加一个新字符。
Pangram :pangram 是一个包含英文字母表中每个字母的句子。
注意:鉴于字符串的长度总是大于 26,并且在一个操作中,我们必须删除现有元素以添加新元素。
例子

Input : str = "qwqqwqeqqwdsdadsdasadsfsdsdsdasasas"
        K = 4
Output : False
Explanation : Making just 4 modifications in this string, 
it can't be changed to a pangram. 

Input : str = "qwqqwqeqqwdsdadsdasadsfsdsdsdasasas"
        K = 24
Output : True
Explanation : By making 19 modifications in the string, 
it can be changed to a pangram.

推荐:请先在“练习”上解决,然后再继续解决。

方法:

  1. 使用布尔访问数组逐个字符遍历字符串以跟踪数组字符。
  2. 使用变量计数,遍历访问数组以保持缺失字符的计数。
  3. 如果计数值小于或等于 K,则打印 True。
  4. 否则打印 False。

以下是上述方法的实现:

C++
// C++ program to check if a
// String can be converted
// to Pangram by atmost k modifications
#include
using namespace std;
 
// Function to find if string
// can be converted to Pangram
// by atmost k modifications
bool isPangram(string S, int k)
{
    if (S.length() < 26)
        return false;
 
    // visit array to keep track
    // of all the characters
    // present in the array
    int visited[26];
 
    for(int i = 0; i < S.length(); i++)
        visited[S[i] - 'a'] = true;
 
    // A variable to keep count
    // of characters missing
    // in the string
    int count = 0;
 
    for(int i = 0; i < 26; i++)
    {
        if (!visited[i])
            count += 1;
    }
 
    // Comparison of count
    // with given value K
    if(count <= k )
        return true;
    return false;
}
         
// Driver Code
int main()
{
 
    string S = "thequickquickfoxmumpsoverthelazydog";
    int k = 15;
     
    // function calling
    isPangram(S, k) ? cout<< "true" :
                      cout<< "false";
 
    return 0;
}
 
// This code is contributed by ChitraNayal


Java
// Java Program to check if a String can be
// converted to Pangram by atmost k modifications
 
public class GFG {
 
    // Function to find if string can be converted
    // to Pangram by atmost k modifications
    static boolean isPangram(String S, int k)
    {
        if (S.length() < 26)
            return false;
 
        // visit array to keep track of all
        // the characters present in the array
        boolean[] visited = new boolean[26];
 
        for (int i = 0; i < S.length(); i++) {
            visited[S.charAt(i) - 'a'] = true;
        }
 
        // A variable to keep count of
        // characters missing in the string
        int count = 0;
 
        for (int i = 0; i < 26; i++) {
            if (!visited[i])
                count++;
        }
         
        // Comparison of count with given value K
        if (count <= k)
            return true;
        return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        String S = "thequickquickfoxmumpsoverthelazydog";
         
        int k = 15;
         
        System.out.print(isPangram(S, k));
    }
}


Python 3
# Python 3 program to check
# if a String can be converted
# to Pangram by atmost k modifications
 
# Function to find if string
# can be converted to Pangram
# by atmost k modifications
def isPangram(S, k) :
 
    if len(S) < 26 :
        return False
 
    # visit array to keep track
    # of all the characters
    # present in the array
    visited = [0] * 26
 
    for char in S :
        visited[ord(char) - ord('a')] = True
 
    # A variable to keep count
    # of characters missing
    # in the string
    count = 0
 
    for i in range(26) :
 
        if visited[i] != True :
            count += 1
 
    # Comparison of count
    # with given value K
    if count <= k :
        return True
    return False
         
# Driver Code
if __name__ == "__main__" :
     
    S = "thequickquickfoxmumpsoverthelazydog"
    k = 15
     
    # function calling
    print(isPangram(S,k))
         
# This code is contributed by ANKITRAI1


C#
// C# Program to check if a
// String can be converted to
// Pangram by atmost k modifications
using System;
 
class GFG
{
 
// Function to find if string
// can be converted to Pangram
// by atmost k modifications
static bool isPangram(String S, int k)
{
    if (S.Length < 26)
        return false;
 
    // visit array to keep track
    // of all the characters present
    // in the array
    bool[] visited = new bool[26];
 
    for (int i = 0; i < S.Length; i++)
    {
        visited[S[i] - 'a'] = true;
    }
 
    // A variable to keep count
    // of characters missing in
    // the string
    int count = 0;
 
    for (int i = 0; i < 26; i++)
    {
        if (!visited[i])
            count++;
    }
     
    // Comparison of count with
    // given value K
    if (count <= k)
        return true;
    return false;
}
 
// Driver code
public static void Main()
{
    string S = "thequickquickfoxmumpsoverthelazydog";
     
    int k = 15;
     
    Console.WriteLine(isPangram(S, k));
}
}
 
// This code is contributed
// by inder_verma.


PHP


Javascript


输出:
true