Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Check if a string is Pangrammatic Lipogram
Next article icon

Missing characters to make a string Pangram

Last Updated : 19 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
 
    // add each character to the set
    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]));
        }
    }
 
    // check which characters are missing
    string missing_chars = "";
    for(char c='a'; c<='z'; c++){
        if(present_chars.find(c) == present_chars.end()){
            missing_chars += c;
        }
    }
 
    // print the missing characters
    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<>();
 
        // add each character to the set
        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));
            }
        }
 
        // check which characters are missing
        StringBuilder missingChars = new StringBuilder();
        for(char c = 'a'; c <= 'z'; c++){
            if(!presentChars.contains(c)){
                missingChars.append(c);
            }
        }
 
        // print the missing characters
        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"
     
    # Use a set to store present characters
    present_chars = set()
 
    # Add each character to the set
    for char in str_:
        if char.isalpha():
            present_chars.add(char.lower())
 
    # Check which characters are missing
    missing_chars = "".join(c for c in "abcdefghijklmnopqrstuvwxyz" if c not in present_chars)
 
    # Print the missing characters
    if not missing_chars:
        print("The string is a pangram.")
    else:
        print(missing_chars)
 
if __name__ == "__main__":
    main()
 
# This Code Is Contributed By Shubham Tiwari
 
 

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>();
 
        // add each character to the set
        foreach (char c in str) {
            if (Char.IsLetter(c)) {
                presentChars.Add(Char.ToLower(c));
            }
        }
 
        // check which characters are missing
        string missingChars = "";
        for (char c='a'; c<='z'; c++) {
            if (!presentChars.Contains(c)) {
                missingChars += c;
            }
        }
 
        // print the missing characters
        if (missingChars.Length == 0) {
            Console.WriteLine("The string is a pangram.");
        }
        else {
            Console.WriteLine(missingChars);
        }
    }
}
// This Code Is Contributed By Shubham Tiwari
 
 

Javascript




// Function to find missing characters in a string to make it a pangram
function findMissingChars(str) {
    const presentChars = new Set();
 
    // Add each character to the set
    for (let i = 0; i < str.length; i++) {
        const char = str[i].toLowerCase();
        if (char >= 'a' && char <= 'z') {
            presentChars.add(char);
        }
    }
 
    // Check which characters are missing
    let missingChars = '';
    for (let c = 'a'; c <= 'z'; c = String.fromCharCode(c.charCodeAt(0) + 1)) {
        if (!presentChars.has(c)) {
            missingChars += c;
        }
    }
 
    // Print the missing characters or indicate that the string is a pangram
    if (missingChars.length === 0) {
        console.log('The string is a pangram.');
    } else {
        console.log(missingChars);
    }
}
 
// Example usage
const str = "The quick brown fox jumps over the dog";
findMissingChars(str);
 
 
Output
alyz       

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++




// C++ program to find characters that needs
// to be added to make Pangram
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
 
// Returns characters that needs to be added
// to make str
string missingChars(string str)
{
    // A boolean array to store characters
    // present in string.
    bool present[MAX_CHAR] = {false};
 
    // Traverse string and mark characters
    // present in string.
    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;
    }
 
    // Store missing characters in alphabetic
    // order.
    string res = "";
    for (int i=0; i<MAX_CHAR; i++)
        if (present[i] == false)
            res.push_back((char)(i+'a'));
 
    return res;
}
 
// Driver program
int main()
{
    string str = "The quick brown fox jumps "
                 "over the dog";
    cout << missingChars(str);
    return 0;
}       
 
 

Java




// Java program to find characters that
// needs to be added to make Pangram
import java.io.*;
import java.util.ArrayList;
 
class GFG{
     
private static ArrayList<Character>missingChars(
    String str, int strLength)
{
    final int MAX_CHARS = 26;
     
    // A boolean array to store characters
    // present in string.
    boolean[] present = new boolean[MAX_CHARS];
    ArrayList<Character> charsList = new ArrayList<>();
     
    // Traverse string and mark characters
    // present in string.
    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;
    }
     
    // Store missing characters in alphabetic
    // order.
    for(int i = 0; i < MAX_CHARS; i++)
    {
        if (present[i] == false)
            charsList.add((char)(i + 'a'));
    }
    return charsList;
}
 
// Driver Code
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);
        }
    }
}
}
 
// This code is contributed by theSardul
 
 

Python3




# Python3 program to find characters
# that needs to be added to make Pangram
MAX_CHAR = 26
 
# Returns characters that needs
# to be added to make str
def missingChars(Str):
     
    # A boolean array to store characters
    # present in string.
    present = [False for i in range(MAX_CHAR)]
 
    # Traverse string and mark characters
    # present in string.
    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
 
    # Store missing characters in alphabetic
    # order.
    res = ""
 
    for i in range(MAX_CHAR):
        if (present[i] == False):
            res += chr(i + ord('a'))
             
    return res
 
# Driver code
Str = "The quick brown fox jumps over the dog"
 
print(missingChars(Str))
 
# This code is contributed by avanitrachhadiya2155
 
 

C#




// C# program to find characters that
// needs to be added to make Pangram
using System.Collections.Generic;
using System;
class GFG{
     
static List<char>missingChars (String str,
                               int strLength)
{
  int MAX_CHARS = 26;
 
  // A boolean array to store
  // characters present in string.
  bool[] present = new bool[MAX_CHARS];
  List<char>charsList = new List<char>();
 
  // Traverse string and mark
  // characters present in string.
  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;
  }
 
  // Store missing characters
  // in alphabetic order.
  for(int i = 0; i < 26; i++)
  {
    if (present[i] == false)
    {
      charsList.Add((char)(i + 'a'));
    }
  }
  return charsList;
}
 
// Driver Code
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);
    }
  }
}
}
 
// This code is contributed by Stream_Cipher
 
 

Javascript




<script>
    // Javascript program to find characters that
    // needs to be added to make Pangram
     
    function missingChars (str, strLength)
    {
      let MAX_CHARS = 26;
 
      // A boolean array to store
      // characters present in string.
      let present = new Array(MAX_CHARS);
      present.fill(false);
      let charsList = [];
 
      // Traverse string and mark
      // characters present in string.
      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;
      }
 
      // Store missing characters
      // in alphabetic order.
      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]);
      }
    }
 
// This code is contributed by mukesh07.
</script>
 
 
Output
alyz       

Time Complexity: O(n) 
Auxiliary Space: O(1)



Next Article
Check if a string is Pangrammatic Lipogram
author
kartik
Improve
Article Tags :
  • DSA
  • Strings
  • Basic Coding Problems
Practice Tags :
  • Strings

Similar Reads

  • Cost to make a string Pangram
    Given an array arr[] containing the cost of adding each alphabet from (a - z) and a string str which may or may not be a Pangram. The task is to find the total cost to make the string Pangram. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
    12 min read
  • Cost to make a string Panagram | Set 2
    Given an array cost[] containing the cost of adding each alphabet from (a – z) and a string str consisting of lowercase English alphabets which may or may not be a Panagram. The task is to make the given string a Panagram with the following operations: Adding a character in str costs twice the cost
    8 min read
  • Count palindromic characteristics of a String
    Given a string s of length n, count the number of substrings having different types of palindromic characteristics. Palindromic Characteristic is the number of k-palindromes in a string where k lies in range [0, n). A string is 1-palindrome(or simply palindrome) if and only if it reads the same back
    15+ min read
  • 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 co
    6 min read
  • Check if a string is Pangrammatic Lipogram
    To understand what a pangrammatic lipogram is we will break this term down into 2 terms i.e. a pangram and a lipogram Pangram: A pangram or holoalphabetic sentence is a sentence using every letter of a given alphabet at least once. The best-known English pangram is "The quick brown fox jumps over th
    14 min read
  • String with k distinct characters and no same characters adjacent
    Given n and k, print a string that has n characters. The string should have exactly k distinct characters and no adjacent positions. Examples: Input : n = 5, k = 3 Output : abcab Explanation: 3 distinct character a, b, c and n length string. Input: 3 2 Output: aba Explanation: 2 distinct character '
    6 min read
  • Python set to check if string is pangram
    Given a string, check if the given string is a pangram or not. Examples: Input : The quick brown fox jumps over the lazy dog Output : The string is a pangram Input : geeks for geeks Output : The string is not pangram A normal way would have been to use frequency table and check if all elements were
    2 min read
  • Minimum number of Appends needed to make a string palindrome
    Given a string s, the task is to find the minimum characters to be appended (insertion at the end) to make a string palindrome. Examples: Input: s = "abede"Output : 2Explanation: We can make string palindrome as "abedeba" by adding ba at the end of the string.Input: s = "aabb"Output : 2Explanation:
    12 min read
  • Program to count vowels, consonant, digits and special characters in string.
    Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels:
    6 min read
  • Rearrange characters to form palindrome if possible
    Given a string, convert the string to palindrome without any modifications like adding a character, removing a character, replacing a character etc. Examples: Input : "mdaam" Output : "madam" or "amdma" Input : "abb" Output : "bab" Input : "geeksforgeeks" Output : "No Palindrome"Count occurrences of
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences