Open In App

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 contains all the characters from ‘a’ to ‘z’ as ’h’ is missing

Input: “Five or six big jet planes zoomed quickly by the tower.”
Output: is a Pangram
Explanation: Contains all the characters from ‘a’ to ‘z’

Boolean Array to Check given string is Pangram or not

In this approach, we will create a boolean array and initially mark their 26 spaces with false, and then we will iterate over the string, and once we get any character, we will make the number of that letter true.

Syntax:

mark = new Array(26).fill(false);

Example: Below is the Implementation of above approach

// A JavaScript Program to check if the given
// String is a pangram or not

// Returns true if the string is pangram else false
function checkPangram(str) {
    str = str.toLowerCase();

    // Create a hash table to mark the characters
    // Present in the string
    mark = new Array(26).fill(false);

    // For indexing in mark[]
    let index;

    // Traverse all characters
    for (
        let i = 0;
        i < str.length;
        i++
    ) {
        if (
            "a" <= str[i] &&
            str[i] <= "z"
        )
            index =
                str.charCodeAt(i) -
                "a".charCodeAt(0);
        // If this character is other than english
        // lowercase characters.
        else continue;

        mark[index] = true;
    }

    // Return false if any character is unmarked
    for (let i = 0; i <= 25; i++)
        if (mark[i] == false)
            return false;

    // If all characters were present
    return true;
}

// Driver Program to test above functions
let str =
    "When zombies arrive, quickly fax Judge Pat";

if (checkPangram(str) == true)
    console.log(str + "\nis a pangram");
else
    console.log(
        str + "\nis not a pangram"
    );

Output
When zombies arrive, quickly fax Judge Pat
is a pangram

Time Complexity : O(n), where n is the length of our string

Auxiliary Space: O(1), as 26 size Boolean vector is constant.

JavaScript Set to Check given string is Pangram or not

In this approach, we will create an empty set, then traverse the whole string and add each element to the set. After traversal, if the size of the set is 26, then we will consider it a pangram string.

Syntax:

const set = new Set();

Example: Below is the code for the following approach

// A JavaScript Program to check if the given
// String is a pangram or not

// Returns true if the string is pangram else false
function checkPangram(str) {
    str = str.toLowerCase();

    // Initialize a set of characters
    const set = new Set();

    for (let ch of str) {
        // If the character is already
        // a lowercase character
        if (ch >= "a" && ch <= "z") {
            set.add(ch);
        }
    }

    // Check if the size is 26 or not
    return set.size === 26;
}

// Driver Program to test above functions
let str =
    "When zombies arrive, quickly fax Judge Pat";

if (checkPangram(str) == true)
    console.log(str + "\nis a pangram");
else
    console.log(
        str + "\nis not a pangram"
    );

Output
When zombies arrive, quickly fax Judge Pat
is a pangram

Time Complexity : O(NlogN), for traversing whole string and inserting all the characters at worst case takes logN time.

Space Complexity : O(1), using constant space for 26 characters at most.

Regular Expression to Check given string is Pangram or not

This approach involves creating a regex pattern that matches each unique letter of the alphabet in the input string. These approach check if all 26 letters of the alphabet are present in the input string, regardless of case and non-alphabetic characters.

Syntax:

const regex = /[a-z]/gi;

Example: Below example implements the above approach

function isPangram(str) {
    const regex = /([a-z])(?!.*\1)/g;
    return (str.toLowerCase().match(regex) || []).length === 26;
}

const str = 'The quick brown fox jumps over the lazy dog';
console.log(`The string "${str}" is ${isPangram(str) ? 'a pangram' : 'not a pangram'}.`);

Output
The string "The quick brown fox jumps over the lazy dog" is a pangram.

Time Complexity: O(n), for Matching the regex pattern against the input string using match(regex).

Space Complexity: O(1), for storing the matched substrings.

Article Tags :