Check if Pangram

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        # Create a set of all lowercase English letters
        alphabet = set('abcdefghijklmnopqrstuvwxyz')

        # Convert the sentence into a set of unique letters
        sentence_letters = set(sentence)

        # Check if every letter in the alphabet is present in the sentence
        is_pangram = alphabet.issubset(sentence_letters)

        return is_pangram
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# @param {String} sentence
# @return {Boolean}
def check_if_pangram(sentence)
  letters = sentence.chars
  array = Array.new(26, 0)
  
  letters.each do |letter|
    array['a'.ord - letter.ord] += 1  
  end 
  
  for i in (0..letters.size-1)
    if array[i] == 0
      return false
    end
  end
  
  return true
end

title: Check if the Sentence Is Pangram tags: array-frequency-counter ascii-code

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 the 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 = "codingskill"
Output: false

Constraints

  • 1 <= sentence.length <= 1000
  • sentence consists of lowercase English letters.
  • Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way).

Check if the number of found characters equals the alphabet length.

Implementation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# @param {String} sentence
# @return {Boolean}
def check_if_pangram(sentence)
  letters = sentence.chars
  array = Array.new(26, 0)
  
  letters.each do |letter|
    array['a'.ord - letter.ord] += 1  
  end 
  
  for i in (0..letters.size-1)
    if array[i] == 0
      return false
    end
  end
  
  return true
end

Building Blocks

  • Array Frequency Counter
  • ASCII Codes