Missing characters to make a string Pangram
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:
Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.
Input : welcome to geeksforgeeks Output : abdhijnpquvxyz Input : The quick brown fox jumps Output : adglvyz
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 strstring 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 programint 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 Pangramimport 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 Codepublic 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 PangramMAX_CHAR = 26# Returns characters that needs# to be added to make strdef 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 elif (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 codeStr = "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 Pangramusing 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 Codepublic 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)
https://youtu.be/VqGOxv8Fb1E



