Check if a String can be converted to Pangram in K changes
Last Updated :
01 Sep, 2022
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.
Input : str = "qwqqwqeqqwdsdadsdasadsfsdsdsdasasas"
K = 24
Output : True
Explanation : By making 19 modifications in the string,
it can be changed to a pangram.
Approach:
- Traverse the string character by character to keep track of all the characters present in the array using a boolean visit array.
- Using a variable count, traverse the visit array to keep count of the missing characters.
- If count value is less than or equal to K, print True.
- Else print False.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isPangram(string S, int k)
{
if (S.length() < 26)
return false ;
int visited[26];
for ( int i = 0; i < S.length(); i++)
visited[S[i] - 'a' ] = true ;
int count = 0;
for ( int i = 0; i < 26; i++)
{
if (!visited[i])
count += 1;
}
if (count <= k )
return true ;
return false ;
}
int main()
{
string S = "thequickquickfoxmumpsoverthelazydog" ;
int k = 15;
isPangram(S, k) ? cout<< "true" :
cout<< "false" ;
return 0;
}
|
Java
public class GFG {
static boolean isPangram(String S, int k)
{
if (S.length() < 26 )
return false ;
boolean [] visited = new boolean [ 26 ];
for ( int i = 0 ; i < S.length(); i++) {
visited[S.charAt(i) - 'a' ] = true ;
}
int count = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
if (!visited[i])
count++;
}
if (count <= k)
return true ;
return false ;
}
public static void main(String[] args)
{
String S = "thequickquickfoxmumpsoverthelazydog" ;
int k = 15 ;
System.out.print(isPangram(S, k));
}
}
|
Python 3
def isPangram(S, k) :
if len (S) < 26 :
return False
visited = [ 0 ] * 26
for char in S :
visited[ ord (char) - ord ( 'a' )] = True
count = 0
for i in range ( 26 ) :
if visited[i] ! = True :
count + = 1
if count < = k :
return True
return False
if __name__ = = "__main__" :
S = "thequickquickfoxmumpsoverthelazydog"
k = 15
print (isPangram(S,k))
|
C#
using System;
class GFG
{
static bool isPangram(String S, int k)
{
if (S.Length < 26)
return false ;
bool [] visited = new bool [26];
for ( int i = 0; i < S.Length; i++)
{
visited[S[i] - 'a' ] = true ;
}
int count = 0;
for ( int i = 0; i < 26; i++)
{
if (!visited[i])
count++;
}
if (count <= k)
return true ;
return false ;
}
public static void Main()
{
string S = "thequickquickfoxmumpsoverthelazydog" ;
int k = 15;
Console.WriteLine(isPangram(S, k));
}
}
|
PHP
<?php
function isPangram( $S , $k )
{
if ( strlen ( $S ) < 26)
return false;
$visited = array_fill (0, 26, NULL);
for ( $i = 0; $i < strlen ( $S ); $i ++)
$visited [ord( $S [ $i ]) -
ord( 'a' )] = true;
$count = 0;
for ( $i = 0; $i < 26; $i ++)
{
if ( $visited [ $i ] != true)
$count += 1;
}
if ( $count <= $k )
return true;
return false;
}
$S = "thequickquickfoxmumpsoverthelazydog" ;
$k = 15;
echo isPangram( $S , $k )? "true" : "false" ;
?>
|
Javascript
<script>
function isPangram(S, k) {
if (S.length < 26) return false ;
var visited = new Array(26);
for ( var i = 0; i < S.length; i++) {
visited[S[i].charCodeAt(0) - "a" .charCodeAt(0)] = true ;
}
var count = 0;
for ( var i = 0; i < 26; i++) {
if (!visited[i]) count++;
}
if (count <= k) return true ;
return false ;
}
var S = "thequickquickfoxmumpsoverthelazydog" ;
var k = 15;
document.write(isPangram(S, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(|S|) ,where S is the given string
- Space Complexity : O(26) ,to store characters.
Share your thoughts in the comments
Please Login to comment...