Member-only story
Coding Interview Question: Find if a string is a pangram
1 min readJun 27, 2024
Prompt
Solution 1
First convert the string to all lowercase letters. Using regex, extract only the letters. Then, create a Set. A set will get the unique values. Then you compare the set with the number of alphabets total which is 26.
const ALPHABET_LENGTH = 26
function pangrams(s) {
const lowercase = s.toLowerCase()
const lettersOnly = lowercase.match(/[a-z]/g)
const uniques = new Set(lettersOnly)
if (uniques.size == ALPHABET_LENGTH){
return "pangram";
} else {
return "not pangram";
}
}
Solution 2
This is a less simple approach but it still a correct solution. This is what I wrote as a solution before going with Solution 1 above.
function pangrams(s) {
// Write your code here
console.log(s);
const alphabetArr = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(" ");
let found = true;
for (let i=0 ; i<alphabetArr.length; i++){
const letter = alphabetArr[i];
if (!s.includes(letter) && !s.includes(letter.toUpperCase())){
found = false;
}
}
if (found){
return "pangram";
} else {
return "not pangram"
}
}