1832. Check if the Sentence Is Pangram
Check if the Sentence Is Pangram
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
Constraints:
1 <= sentence.length <= 1000
sentence
consists of lowercase English letters.
Approach) Brute Force
/**
* @param {string} sentence
* @return {boolean}
*/
var checkIfPangram = function(sentence) {
let arr = Array(26).fill(false);
for (let char of sentence) {
let code = char.charCodeAt(0);
arr[code - 97] = true;
}
for (let i = 0; i < arr.length; i++) {
if (!arr[i]) {
return false;
}
}
return true;
};
var checkIfPangram = function(sentence) {
let arr = [];
for(let i=0; i<sentence.length && arr.length<26; i++) {
if( !arr.includes(sentence[i]) ) {
arr.push(sentence[i]);
}
}
if(arr.length == 26) return true;
return false;
};
/**
* @param {string} sentence
* @return {boolean}
*/
var checkIfPangram = function(sentence) {
let s = new Set()
for(let i=0; i<sentence.length; i++){
s.add(sentence[i])
}
return s.size === 26;
};
/**
* @param {string} sentence
* @return {boolean}
*/
var checkIfPangram = function(sentence) {
return (sentence) => [...new Set(sentence)].length === 26;
};
/**
* @param {string} sentence
* @return {boolean}
*/
var checkIfPangram = function(sentence) {
let map=new Map()
for(let x of sentence){
map.set(x,map.get(x)+1||1)
}
return map.size>=26
};
var checkIfPangram = function(sentence) {
const hashmap = new Map();
for (let i = 0; i < sentence.length; i++) {
if (!hashmap.has(sentence[i])) {
hashmap.set(sentence[i], 1);
}
if (hashmap.size === 26) return true;
}
return false;
};
That’s all folks! In this post, we solved LeetCode problem #1832. Check if the Sentence Is Pangram
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment