Solving Leetcode Interviews in Seconds with AI: Check if the Sentence Is Pangram
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1832" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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.
Explanation
Here's the solution:
- High-Level Approach: Use a set to keep track of the unique characters encountered in the sentence. If the size of the set becomes 26, it means all letters of the English alphabet are present.
Efficiency: This avoids unnecessary iterations or comparisons once all characters are found.
Complexity:
- Runtime: O(n), where n is the length of the sentence.
- Storage: O(1), because the set can hold at most 26 characters.
Code
def checkIfPangram(sentence: str) -> bool:
"""
Checks if a string is a pangram (contains all letters of the English alphabet).
Args:
sentence: The input string containing lowercase English letters.
Returns:
True if the sentence is a pangram, False otherwise.
"""
seen = set()
for char in sentence:
seen.add(char)
if len(seen) == 26:
return True
return False