# Check if a String can be converted to Pangram in K changes

Given a String str containing only lowercase English alphabets and an integer K. The task is to check that whether the string can be converted to a Pangram by performing at most K changes. In one change we can remove any existing character and add a new character.

Pangram: A pangram is a sentence containing every letter in the English Alphabet.

Note: Given that length of string is greater than 26 always and in one operation we have to remove an existing element to add a new element.

Examples:

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

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

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Approach:

1. Traverse the string character by character to keep track of all the characters present in the array using a boolean visit array.
2. Using a variable count, traverse the visit array to keep count of the missing characters.
3. If count value is less than or equal to K, print True.
4. Else print False.

Below is the implementation of above approach:

## 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;  ` ` `  `    ``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``;  ` ` `  `    ``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

 ` `

Output:

```true
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.