Coding problem · Easy · Basics
Is It a Pangram?
Easy Algorithms & data structures
A pangram contains every letter from a to z at least once (case doesn't matter; other characters are ignored). Return true if s is a pangram.
Example 1
Input: s = "The quick brown fox jumps over the lazy dog" Output: true
Example 2
Input: s = "Hello world" Output: false
Hints
Hint 1
Collect the distinct letters you see in a set.
Hint 2
It's a pangram when the set holds 26 letters.
Write and run your solution
Loading the editor… (JavaScript is required to run code in your browser.)
Solution and explanation
Show the solution
Time O(n) · Space O(1)
Add each lower-cased letter to a set and compare its size with 26. Ignore spaces and punctuation.
Reference solution
function isPangram(s) {
const letters = new Set(s.toLowerCase().replace(/[^a-z]/g, ''));
return letters.size === 26;
}
function isPangram(s: string): boolean {
const letters = new Set(s.toLowerCase().replace(/[^a-z]/g, ''));
return letters.size === 26;
}
def is_pangram(s: str) -> bool:
return len({ch for ch in s.lower() if 'a' <= ch <= 'z'}) == 26
<?php
function isPangram(string $s): bool
{
return count(array_unique(str_split(preg_replace('/[^a-z]/', '', strtolower($s)) ?: ' '))) === 26;
}
class Solution {
public boolean isPangram(String s) {
java.util.Set<Character> seen = new java.util.HashSet<>();
for (char ch : s.toLowerCase().toCharArray()) if (ch >= 'a' && ch <= 'z') seen.add(ch);
return seen.size() == 26;
}
}
More Basics problems
- Reverse a String · Easy
- Count the Vowels · Easy
- Capitalize Each Word · Easy
- Longest Word in a Sentence · Easy
- Run-Length Encoding · Easy
- Make an Acronym · Easy