Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order.
Examples:
Input : welcome to geeksforgeeks
Output : abdhijnpquvxyz
Input : The quick brown fox jumps
Output : adglvyz
Approach 1: Using a set to keep track of present characters
- Initialize an empty set to keep track of the present characters.
- Iterate through each character in the input string.
- If the character is a lowercase letter, add it to the set.
- If the character is an uppercase letter, convert it to lowercase and add it to the set.
- Initialize an empty string to store the missing characters.
- Iterate through each lowercase letter from ‘a’ to ‘z’.
- If the letter is not in the set, append it to the missing characters string.
- If the missing characters string is empty, the input string is a pangram.
- Otherwise, the missing characters string contains the characters that need to be added to the input string to make it a pangram. Print this string as the output.
C++
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
int main() {
string str = "The quick brown fox jumps over the dog" ;
set< char > present_chars;
for ( int i=0; i<str.length(); i++){
if (str[i] >= 'a' && str[i] <= 'z' ){
present_chars.insert(str[i]);
}
else if (str[i] >= 'A' && str[i] <= 'Z' ){
present_chars.insert( tolower (str[i]));
}
}
string missing_chars = "" ;
for ( char c= 'a' ; c<= 'z' ; c++){
if (present_chars.find(c) == present_chars.end()){
missing_chars += c;
}
}
if (missing_chars.length() == 0){
cout << "The string is a pangram." << endl;
}
else {
cout << missing_chars << endl;
}
return 0;
}
|
Java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
String str = "The quick brown fox jumps over the dog" ;
HashSet<Character> presentChars = new HashSet<>();
for ( int i = 0 ; i < str.length(); i++){
char c = str.charAt(i);
if (c >= 'a' && c <= 'z' ){
presentChars.add(c);
}
else if (c >= 'A' && c <= 'Z' ){
presentChars.add(Character.toLowerCase(c));
}
}
StringBuilder missingChars = new StringBuilder();
for ( char c = 'a' ; c <= 'z' ; c++){
if (!presentChars.contains(c)){
missingChars.append(c);
}
}
if (missingChars.length() == 0 ){
System.out.println( "The string is a pangram." );
}
else {
System.out.println(missingChars);
}
}
}
|
Python
def main():
str_ = "The quick brown fox jumps over the dog"
present_chars = set ()
for char in str_:
if char.isalpha():
present_chars.add(char.lower())
missing_chars = " ".join(c for c in " abcdefghijklmnopqrstuvwxyz" if c not in present_chars)
if not missing_chars:
print ( "The string is a pangram." )
else :
print (missing_chars)
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void Main() {
string str = "The quick brown fox jumps over the dog" ;
HashSet< char > presentChars = new HashSet< char >();
foreach ( char c in str) {
if (Char.IsLetter(c)) {
presentChars.Add(Char.ToLower(c));
}
}
string missingChars = "" ;
for ( char c= 'a' ; c<= 'z' ; c++) {
if (!presentChars.Contains(c)) {
missingChars += c;
}
}
if (missingChars.Length == 0) {
Console.WriteLine( "The string is a pangram." );
}
else {
Console.WriteLine(missingChars);
}
}
}
|
Javascript
function findMissingChars(str) {
const presentChars = new Set();
for (let i = 0; i < str.length; i++) {
const char = str[i].toLowerCase();
if (char >= 'a' && char <= 'z' ) {
presentChars.add(char);
}
}
let missingChars = '' ;
for (let c = 'a' ; c <= 'z' ; c = String.fromCharCode(c.charCodeAt(0) + 1)) {
if (!presentChars.has(c)) {
missingChars += c;
}
}
if (missingChars.length === 0) {
console.log( 'The string is a pangram.' );
} else {
console.log(missingChars);
}
}
const str = "The quick brown fox jumps over the dog" ;
findMissingChars(str);
|
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
We have discussed Pangram Checking. The idea is similar, we traverse a given string and mark all visited characters. In the end, we print all those characters which are not visited.
Lowercase and Uppercase characters are considered the same.
C++
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
string missingChars(string str)
{
bool present[MAX_CHAR] = { false };
for ( int i=0; i<str.length(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z' )
present[str[i]- 'a' ] = true ;
else if (str[i] >= 'A' && str[i] <= 'Z' )
present[str[i]- 'A' ] = true ;
}
string res = "" ;
for ( int i=0; i<MAX_CHAR; i++)
if (present[i] == false )
res.push_back(( char )(i+ 'a' ));
return res;
}
int main()
{
string str = "The quick brown fox jumps "
"over the dog" ;
cout << missingChars(str);
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
class GFG{
private static ArrayList<Character>missingChars(
String str, int strLength)
{
final int MAX_CHARS = 26 ;
boolean [] present = new boolean [MAX_CHARS];
ArrayList<Character> charsList = new ArrayList<>();
for ( int i = 0 ; i < strLength; i++)
{
if ( 'A' <= str.charAt(i) &&
str.charAt(i) <= 'Z' )
present[str.charAt(i) - 'A' ] = true ;
else if ( 'a' <= str.charAt(i) &&
str.charAt(i) <= 'z' )
present[str.charAt(i) - 'a' ] = true ;
}
for ( int i = 0 ; i < MAX_CHARS; i++)
{
if (present[i] == false )
charsList.add(( char )(i + 'a' ));
}
return charsList;
}
public static void main(String[] args)
{
String str = "The quick brown fox jumps " +
"over the dog" ;
ArrayList<Character> missing = GFG.missingChars(
str, str.length());
if (missing.size() >= 1 )
{
for (Character character : missing)
{
System.out.print(character);
}
}
}
}
|
Python3
MAX_CHAR = 26
def missingChars( Str ):
present = [ False for i in range (MAX_CHAR)]
for i in range ( len ( Str )):
if ( Str [i] > = 'a' and Str [i] < = 'z' ):
present[ ord ( Str [i]) - ord ( 'a' )] = True
else if ( Str [i] > = 'A' and Str [i] < = 'Z' ):
present[ ord ( Str [i]) - ord ( 'A' )] = True
res = ""
for i in range (MAX_CHAR):
if (present[i] = = False ):
res + = chr (i + ord ( 'a' ))
return res
Str = "The quick brown fox jumps over the dog"
print (missingChars( Str ))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static List< char >missingChars (String str,
int strLength)
{
int MAX_CHARS = 26;
bool [] present = new bool [MAX_CHARS];
List< char >charsList = new List< char >();
for ( int i = 0; i < strLength; i++)
{
if ( 'A' <= str[i] &&
str[i] <= 'Z' )
present[str[i] - 'A' ] = true ;
else if ( 'a' <= str[i] &&
str[i] <= 'z' )
present[str[i] - 'a' ] = true ;
}
for ( int i = 0; i < 26; i++)
{
if (present[i] == false )
{
charsList.Add(( char )(i + 'a' ));
}
}
return charsList;
}
public static void Main()
{
String str = "The quick brown fox jumps over the dog" ;
List< char > missing = missingChars(str,
str.Length);
if (missing.Count >= 1)
{
foreach ( var i in missing)
{
Console.Write(i);
}
}
}
}
|
Javascript
<script>
function missingChars (str, strLength)
{
let MAX_CHARS = 26;
let present = new Array(MAX_CHARS);
present.fill( false );
let charsList = [];
for (let i = 0; i < strLength; i++)
{
if ( 'A' .charCodeAt() <= str[i].charCodeAt() && str[i].charCodeAt() <= 'Z' .charCodeAt())
present[str[i].charCodeAt() - 'A' .charCodeAt()] = true ;
else if ( 'a' .charCodeAt() <= str[i].charCodeAt() && str[i].charCodeAt() <= 'z' .charCodeAt())
present[str[i].charCodeAt() - 'a' .charCodeAt()] = true ;
}
for (let i = 0; i < 26; i++)
{
if (present[i] == false )
{
charsList.push(String.fromCharCode(i + 'a' .charCodeAt()));
}
}
return charsList;
}
let str = "The quick brown fox jumps over the dog" ;
let missing = missingChars(str, str.length);
if (missing.length >= 1)
{
for (let i = 0; i < missing.length; i++)
{
document.write(missing[i]);
}
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Oct, 2023
Like Article
Save Article