1 minute read

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

Example 1:

Input: sentence = “thequickbrownfoxjumpsoverthelazydog” Output: true Explanation: sentence contains at least one of every letter of the English alphabet. Example 2:

Input: sentence = “leetcode” Output: false

/**
 * @param {string} sentence
 * @return {boolean}
 */
var checkIfPangram = function (sentence) {
  // Use set to store the unique characters without duplication
  let set = new Set(sentence);

  // If set size is not equal to 26, return false
  if (set.size !== 26) {
    return false;
  } else {
    // If set siez is equal to 26, return true
    return true;
  }
};

Create a Set is used to store the unique characters from the sentence. Check size of the set using set.size. If the size is not equal to 26, you return false. This is because for a sentence to be a pangram, it should contain all 26 letters of the English alphabet, and if the size of the set is not 26, it means some letters are missing. If the size of the set is 26, you return true, indicating that the sentence is a pangram.

The time complexity of the code is O(n), where n is the length of the input sentence, because it iterates through the characters in the sentence to build the set.

Leave a comment