JavaScript Program to Find Missing Characters to Make a String Pangram
Last Updated :
31 Jul, 2024
We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding.
Examples:
Input : welcome to geeksforgeeks
Output : abdhijnpquvxyz
Input : The quick brown fox jumps
Output : adglvyz
Using Set
In this approach, we use the Set in JavaScript to create the characters from the input string and remove the duplicate, and then go through the alphabet characters. Each alphabet letter is been checked if it is present in the Set. If it is not present then we add it to the output variable and printing it.
Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using Set.
JavaScript
let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let inputSet = new Set(inputStr);
let missingChars = [...letters]
.filter(char => !inputSet.has(char)).join('');
console.log(missingChars);
Using includes() Method
In this approach, we are using the includes() method. Here we have used the for loop to go through the each character of the letter variable. For every letter, we are checking if the character is present in the inputStr variable using the includes() method. If the character is not present, then it is added in the output variable and printed.
Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using includes() Method.
JavaScript
let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let output = "";
inputStr = inputStr.toLowerCase();
for (let c of letters) {
if (!inputStr.includes(c)) {
output += c;
}
}
console.log(output);
Using indexOf() Method
In this apporach, we are using the indexOf() method. Here, we have used the for loop to go through the chacter-by-character of the letters string. Every character, we check if the letters variable cgacater exists in the input string using indexOf() method. If the chactaer is found then it is been ignored, if it is not found then the character is added in the output string.
Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using indexOf() method.
JavaScript
let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let output = "";
inputStr = inputStr.toLowerCase();
for (let i = 0; i < letters.length; i++) {
if (inputStr.indexOf(letters[i]) === -1) {
output += letters[i];
}
}
console.log(output);
Using a Map
Using a Map to find missing characters for a pangram involves initializing a map for all alphabet letters, updating it with characters from the string, and then collecting keys from the map with zero occurrences.
Example: In this example we finds missing characters to make a string a pangram using a Map. It initializes the map with alphabet letters, counts occurrences, and identifies missing characters.
JavaScript
function findMissingPangramChars(str) {
const freqMap = new Map();
for (let i = 0; i < 26; i++) {
freqMap.set(String.fromCharCode(97 + i), 0);
}
str.toLowerCase().split('').forEach(char => {
if (freqMap.has(char)) {
freqMap.set(char, freqMap.get(char) + 1);
}
});
const missingChars = [];
for (let [char, count] of freqMap) {
if (count === 0) {
missingChars.push(char);
}
}
return missingChars.join('');
}
console.log(findMissingPangramChars("The quick brown fox jumps over the lazy dog"));
console.log(findMissingPangramChars("Hello, World!"));
Outputabcfgijkmnpqstuvxyz
Using Set and Range of ASCII Values
In this approach, we utilize a set to keep track of characters present in the input string. We iterate through the input string and add each character to the set. Then, we iterate through a range of ASCII values representing lowercase alphabets (97 to 122), checking if each character exists in the set. If not found, it is considered missing and added to the output string.
Example:
JavaScript
function findMissingChars(inputStr) {
let missingChars = '';
const charSet = new Set(inputStr.toLowerCase());
// Iterate through ASCII range of lowercase alphabets
for (let ascii = 97; ascii <= 122; ascii++) {
const char = String.fromCharCode(ascii);
if (!charSet.has(char)) {
missingChars += char;
}
}
return missingChars;
}
const inputStr = "welcome to geeksforgeeks";
console.log(findMissingChars(inputStr)); // Output: abdhijnpquvxyz
Using Regular Expressions
In this approach, we use regular expressions to identify which letters of the alphabet are missing from the input string. We convert the input string to lowercase and create a regular expression that matches any character not found in the string. Finally, we use this regular expression to find and collect the missing characters.
Example:
JavaScript
function findMissingCharsUsingRegex(inputStr) {
const letters = "abcdefghijklmnopqrstuvwxyz";
const lowerInputStr = inputStr.toLowerCase();
let missingChars = '';
// Iterate through each letter in the alphabet
for (let i = 0; i < letters.length; i++) {
const char = letters[i];
const regex = new RegExp(char, 'g');
if (!regex.test(lowerInputStr)) {
missingChars += char;
}
}
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingRegex(inputStr)); // Output: abdhijnpquvxyz
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingRegex(inputStr)); // Output: adglvyz
// Contributed by Nikunj Sonigara
Outputabdhijnpquvxyz
adglvyz
Approach: Using Bitwise Operations
In this approach, we utilize bitwise operations to determine which characters are missing from the input string. Each bit in an integer represents a different letter of the alphabet. By setting bits corresponding to the letters found in the input string, we can efficiently check which letters are missing.
Example:
This example demonstrates the use of bitwise operations to find missing characters in the input string.
JavaScript
function findMissingCharacters(inputStr) {
// Initialize checker to zero
let checker = 0;
// Iterate through each character in the input string
for (let char of inputStr.toLowerCase()) {
if (char >= 'a' && char <= 'z') {
// Calculate the bit position for the character
let bitPosition = char.charCodeAt(0) - 'a'.charCodeAt(0);
// Set the bit at the corresponding position
checker |= (1 << bitPosition);
}
}
// Initialize the result string
let result = '';
// Check for missing characters
for (let i = 0; i < 26; i++) {
// If the bit at position i is not set, add the character to the result
if ((checker & (1 << i)) === 0) {
result += String.fromCharCode('a'.charCodeAt(0) + i);
}
}
return result;
}
// Test cases
console.log(findMissingCharacters("welcome to geeksforgeeks")); // Output: abdhijnpquvxyz
console.log(findMissingCharacters("The quick brown fox jumps")); // Output: ad
Outputabdhijnpquvxyz
adglvyz
Using reduce() Method
In this approach, we use the reduce() method to accumulate the missing characters. We iterate through each character in the alphabet and check if it is present in the input string using the includes() method. If the character is not present, it is added to the accumulator, which holds the missing characters.
Example:
JavaScript
function findMissingCharsUsingReduce(inputStr) {
let letters = "abcdefghijklmnopqrstuvwxyz";
let lowerInputStr = inputStr.toLowerCase();
let missingChars = letters.split('').reduce((acc, char) => {
if (!lowerInputStr.includes(char)) {
acc += char;
}
return acc;
}, '');
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingReduce(inputStr));
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingReduce(inputStr));
Outputabdhijnpquvxyz
adglvyz
Using Array Difference
In this approach, we first create an array of all lowercase letters. Then, we filter out the letters that are present in the input string to get the missing characters.
Example:
JavaScript
function findMissingCharsUsingArrayDifference(inputStr) {
const letters = "abcdefghijklmnopqrstuvwxyz".split('');
const inputChars = inputStr.toLowerCase().split('');
const missingChars = letters.filter(char => !inputChars.includes(char)).join('');
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingArrayDifference(inputStr));
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingArrayDifference(inputStr));
Outputabdhijnpquvxyz
adglvyz
Similar Reads
JavaScript Program to Find Kâth Non-Repeating Character in String
The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps
6 min read
JavaScript Program to Mirror Characters of a String
Our task is to mirror characters from the N-th position up to the end of a given string, where 'a' will be converted into 'z', 'b' into 'y', and so on. This JavaScript problem requires mirroring the characters in a string starting from a specified position. There are various approaches available to
6 min read
JavaScript Program to Search a Word in a 2D Grid of Characters
In this article, we will solve a problem in which you are given a grid, with characters arranged in a two-layout(2D). You need to check whether a given word is present in the grid. A word can be matched in all 8 directions at any point. Word is said to be found in a direction if all characters match
7 min read
JavaScript Program to find Lexicographically next String
In this article, we are going to learn how can we find the Lexicographically next string. Lexicographically next string refers to finding the string that follows a given string in a dictionary or alphabetical order.Examples: Input : testOutput : tesuExplanation : The last character 't' is changed to
3 min read
JavaScript Program to Reverse Consonants in a Given String Without Affecting the Other Elements
In this article, we have given a string and the task is to write a javascript program to reverse only the consonants of a string without affecting the other elementsâ positions and printing the output in the console.Examples:Input: helloOutput: lelhoExplanation: h,l,l are consonants in the given str
6 min read
PHP Program to Find Missing Characters to Make a String Pangram
In this article, we will see how to find the missing characters to make the string Pangram using PHP. A pangram is a sentence containing every letter in the English Alphabet. Examples: Input: The quick brown fox jumps over the dogOutput: Missing Characters - alyzInput: The quick brown fox jumpsOutpu
2 min read
Javascript Program to Check if a given string is Pangram or not
In this article, we are going to implement algorithms to check whether a given string is a pangram or not. Pangram strings are those strings that contain all the English alphabets in them.Example: Input: âFive or six big jet planes zoomed quickly by tower.â Output: is a Pangram Explanation: Does'not
7 min read
Java Program to Separate the Individual Characters from a String
The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided
2 min read
Javascript Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc
3 min read
PHP Program to Check if a Given String is Pangram or Not
A pangram is a sentence that contains every letter of the alphabet at least once. Checking whether a given string is a pangram is a common programming task. In this article, we will explore different approaches to creating a PHP program that checks if a given string is a pangram or not.Table of Cont
4 min read