Check if the Sentence Is Pangram solution and explanation
9 more videos on this problem
Check if the Sentence Is Pangram | 2 Approaches | Snapdeal | Leetcode 1832codestorywithMIK · 11:49 · 23K views
Check if sentence is Pangram (LeetCode 1832) | Full solution with multiple techniquesNikhil Lohia · 12:34 · 13K views
LeetCode 1832: Check if the Sentence Is Pangram SolutionEngineering Digest · 6:30 · 4K views
Leetcode | 1832. Check if the Sentence Is Pangram | Easy | Java SolutionDeveloper Docs · 2:10 · 1K views
Leetcode 1832 Check if the Sentence Is Pangram | Coding Decoded SDE SheetCoding Decoded · 4:23 · 839 views
Check if the Sentence Is Pangram | HashSet | Leetcode 1832 | PythonCheatCode Ninja · 2:55 · 714 views
Check if the Sentence Is Pangram LeetCode 1832 | JavascriptSantamaria Code · 6:05 · 595 views
1832. Check if the Sentence Is Pangram (Leetcode Easy)Programming Live with Larry · 2:21 · 530 views
1832 . Check if the Sentence Is Pangram with python | Leetcode with Python #youtube #youtuberArsalan Ahmed · 6:02 · 434 views
Check if the Sentence Is Pangram | LeetCode 1832 | CppCodeClips with Abhishek Ranjan · 5:57 · 374 views
The problem
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 <= 1000sentenceconsists of lowercase English letters.
Approach overview
Problem Overview: You receive a lowercase sentence and need to determine whether it is a pangram—a string that contains every letter from 'a' to 'z' at least once. The task is essentially checking if all 26 English alphabet characters appear in the string.
Approach 1: Using a Set to Track Letters (O(n) time, O(1) space)
The most straightforward method uses a set (or hash set) to track unique characters encountered while iterating through the sentence. As you scan each character, insert it into the set. Since sets automatically discard duplicates, the structure eventually holds only unique letters. After processing the entire string, check whether the set size equals 26. This approach relies on constant-time hash insertions and lookups, making the scan linear with respect to the sentence length.
This technique is a classic application of a hash table for uniqueness tracking. It’s concise, readable, and widely used in interviews because it directly maps the problem requirement (unique characters) to the right data structure.
Approach 2: Boolean Array for Tracking (O(n) time, O(1) space)
A slightly more memory-efficient method replaces the set with a fixed-size boolean array of length 26. Each index corresponds to a letter: index = char - 'a'. Iterate through the sentence and mark the corresponding index as true when a character appears. After the scan, verify that every position in the array is marked true. If any entry remains false, the sentence is missing that letter.
This approach avoids hashing and uses direct indexing instead. The memory footprint is constant because the array size never exceeds 26. It also tends to run slightly faster in lower-level languages due to predictable memory access patterns. The logic is simple: iterate through the string, mark presence, then verify coverage of the alphabet.
Some implementations optimize further by maintaining a counter of discovered letters. Each time a new letter is marked for the first time, increment the counter. Once it reaches 26, you can terminate early without scanning the rest of the string.
Recommended for interviews: Both solutions run in O(n) time and O(1) space, but the boolean array approach demonstrates stronger understanding of constraints and constant-space optimization. Interviewers often expect the hash set solution first because it’s the most intuitive. Following it with the boolean array optimization shows you can reduce overhead when the input domain (26 letters) is fixed.
Approach 1: Using a Set to Track Letters
This approach leverages a set data structure to track the unique characters. The idea is to iterate through the sentence and add each letter to a set. Finally, we check if the size of the set is 26, which indicates that all letters of the alphabet are present.
def checkIfPangram(sentence: str) -> bool:
return len(set(sentence)) == 26import java.util.HashSet;
public class Solution {
public boolean checkIfPangram(String sentence) {
HashSet<Character> seen = new HashSet<>();
for (char ch : sentence.toCharArray()) {
seen.add(ch);
}
return seen.size() == 26;
}
}#include <string>
#include <unordered_set>
using namespace std;
bool checkIfPangram(string sentence) {
unordered_set<char> seen;
for (char ch : sentence) {
seen.insert(ch);
}
return seen.size() == 26;
}function checkIfPangram(sentence) {
const seen = new Set(sentence);
return seen.size === 26;
}#include <stdbool.h>
#include <string.h>
bool checkIfPangram(char *sentence) {
bool letters[26] = {false};
for (int i = 0; sentence[i] != '\0'; i++) {
letters[sentence[i] - 'a'] = true;
}
for (int i = 0; i < 26; i++) {
if (!letters[i]) return false;
}
return true;
}using System;
using System.Collections.Generic;
public class Solution {
public bool CheckIfPangram(string sentence) {
HashSet<char> seen = new HashSet<char>(sentence);
return seen.Count == 26;
}
}The Python solution takes advantage of the set data type to filter out unique characters in the sentence. If the set length equals 26 after processing the sentence, it indicates a pangram.
Time Complexity: O(n), where n is the string length.
Space Complexity: O(1), as the set contains a limited set of unique characters.
Approach 2: Boolean Array for Tracking
This method uses a boolean array of size 26 to directly map each alphabet character to an index. By iterating over each character in the sentence, we update its corresponding index in the array to true. The sentence is confirmed as a pangram if all indices in the boolean array are true.
def checkIfPangram(sentence: str) -> bool:
seen = [0] * 26
for char in sentence:
seen[ord(char) - ord('a')] = 1
return sum(seen) == 26public class Solution {
public boolean checkIfPangram(String sentence) {
boolean[] alphabet = new boolean[26];
for (char ch : sentence.toCharArray()) {
alphabet[ch - 'a'] = true;
}
for (boolean b : alphabet) {
if (!b) return false;
}
return true;
}
}#include <string>
using namespace std;
bool checkIfPangram(string sentence) {
bool alphabet[26] = {false};
for (char ch : sentence) {
alphabet[ch - 'a'] = true;
}
for (bool exists : alphabet) {
if (!exists) return false;
}
return true;
}function checkIfPangram(sentence) {
let alphabet = new Array(26).fill(false);
for (let char of sentence) {
alphabet[char.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
return alphabet.every(Boolean);
}#include <stdbool.h>
#include <string.h>
bool checkIfPangram(char *sentence) {
bool alphabet[26] = {false};
for (int i = 0; sentence[i] != '\0'; i++) {
alphabet[sentence[i] - 'a'] = true;
}
for (int i = 0; i < 26; i++) {
if (!alphabet[i]) return false;
}
return true;
}public class Solution {
public bool CheckIfPangram(string sentence) {
bool[] alphabet = new bool[26];
foreach (char ch in sentence) {
alphabet[ch - 'a'] = true;
}
foreach (bool b in alphabet) {
if (!b) return false;
}
return true;
}
}The Python application applies a list of integers, functioning similarly to a boolean array. Every encountered letter activates its corresponding index. The sum of the list after traversal will determine the pangram requirement.
Time Complexity: O(n), where n is the length of the input.
Space Complexity: O(1), being constant size — 26 integers.
Approach 3: Array or Hash Table
Traverse the string sentence, use an array or hash table to record the letters that have appeared, and finally check whether there are 26 letters in the array or hash table.
The time complexity is O(n), and the space complexity is O(C). Where n is the length of the string sentence, and C is the size of the character set. In this problem, C = 26.
function checkIfPangram(sentence: string): boolean {
const vis = new Array(26).fill(false);
for (const c of sentence) {
vis[c.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
}
return vis.every(v => v);
}func checkIfPangram(sentence string) bool {
vis := [26]bool{}
for _, c := range sentence {
vis[c-'a'] = true
}
for _, v := range vis {
if !v {
return false
}
}
return true
}impl Solution {
pub fn check_if_pangram(sentence: String) -> bool {
let mut vis = [false; 26];
for c in sentence.as_bytes() {
vis[(*c - b'a') as usize] = true;
}
vis.iter().all(|v| *v)
}
}Approach 4: Bit Manipulation
We can also use an integer mask to record the letters that have appeared, where the i-th bit of mask indicates whether the i-th letter has appeared.
Finally, check whether there are 26 1s in the binary representation of mask, that is, check whether mask is equal to 2^{26} - 1. If so, return true, otherwise return false.
The time complexity is O(n), where n is the length of the string sentence. The space complexity is O(1).
function checkIfPangram(sentence: string): boolean {
let mark = 0;
for (const c of sentence) {
mark |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
}
return mark === (1 << 26) - 1;
}func checkIfPangram(sentence string) bool {
mask := 0
for _, c := range sentence {
mask |= 1 << int(c-'a')
}
return mask == 1<<26-1
}impl Solution {
pub fn check_if_pangram(sentence: String) -> bool {
let mut mark = 0;
for c in sentence.as_bytes() {
mark |= 1 << (*c - b'a');
}
mark == (1 << 26) - 1
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Using a Set to Track Letters | Time Complexity: O(n), where n is the string length. |
| Boolean Array for Tracking | Time Complexity: O(n), where n is the length of the input. |
| Array or Hash Table | – |
| Bit Manipulation | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hash Set to Track Letters | O(n) | O(1) | General case; simplest and most readable approach using a hash set |
| Boolean Array (26 Letters) | O(n) | O(1) | When optimizing for minimal overhead and fixed alphabet size |
Frequently asked questions
Is Check if the Sentence Is Pangram easy or hard?
The problem is classified as Easy because it requires basic string iteration and simple data structures. The main idea is recognizing that only 26 possible letters exist, allowing constant-space tracking with either a set or a boolean array.
Check if the Sentence Is Pangram Python/Java solution
Python solutions typically use a set to store characters and check if its size reaches 26. Java implementations often use either a HashSet<Character> or a boolean array of size 26 with index mapping using char - 'a'. Both implementations achieve O(n) time and O(1) space complexity.
How to solve Check if the Sentence Is Pangram in O(n)?
Iterate through the sentence and track encountered characters using either a hash set or a boolean array of size 26. For each character, mark its presence. After the scan, confirm that all 26 letters are present. Because the string is processed once, the algorithm runs in O(n) time with constant space.
What is the best approach for Check if the Sentence Is Pangram?
The best approach uses a hash set or a fixed boolean array to track which letters appear in the sentence. Both methods scan the string once and verify that all 26 lowercase English letters exist. The boolean array approach is slightly more optimized because it avoids hashing and uses direct indexing.
Is Check if the Sentence Is Pangram asked at Google/Amazon/Meta?
Pangram-style questions appear in coding interviews at major tech companies because they test understanding of strings, hashing, and constant-space optimization. While the exact problem may vary, similar character tracking tasks are commonly asked at companies like Amazon and Google.
What data structure is used in Check if the Sentence Is Pangram?
The most common data structures are a hash set or a fixed boolean array. A hash set stores unique characters encountered during iteration, while a boolean array maps each alphabet letter to an index from 0 to 25 for constant-time marking.
What is the time complexity of Check if the Sentence Is Pangram?
The optimal time complexity is O(n), where n is the length of the sentence. Each character is processed once during the scan. The additional verification step checks a fixed 26-element structure, which is constant time.
