Advertisement

Check if the Sentence Is Pangram - LeetCode 1832 Solution

Check if the Sentence Is Pangram - Complete Solution Guide

Check if the Sentence Is Pangram is LeetCode problem 1832, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3.

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 lo

Detailed Explanation

The problem asks you to determine if a given sentence is a pangram. A pangram is a sentence containing every letter of the English alphabet at least once. The input is a string `sentence` consisting only of lowercase English letters. The output is a boolean value: `true` if the sentence is a pangram, and `false` otherwise. The input string's length is constrained to be between 1 and 1000 characters.

Solution Approach

The provided Python solution uses a very efficient approach. It leverages the properties of Python sets to determine if the sentence is a pangram. First, it converts the input sentence string into a set using `set(sentence)`. This automatically removes duplicate letters. Then, it checks if the size of this set is equal to 26 (the number of letters in the English alphabet). If the size is 26, it means all 26 letters are present, and the function returns `true`; otherwise, it returns `false`.

Step-by-Step Algorithm

  1. Step 1: Convert the input string `sentence` into a set using `set(sentence)`. This creates a set containing only the unique characters from the sentence.
  2. Step 2: Check the length of the set. If `len(set(sentence))` is equal to 26, it means all 26 letters of the English alphabet are present in the sentence.
  3. Step 3: Return `true` if the length of the set is 26, indicating a pangram; otherwise, return `false`.

Key Insights

  • Insight 1: A set can be used to efficiently identify unique characters in the sentence. Since sets only store unique elements, the size of the set representing the sentence will directly indicate whether all 26 letters are present.
  • Insight 2: The Python `set()` function provides a concise way to create a set from an iterable (like a string).
  • Insight 3: No special handling of edge cases is needed because the problem constraints guarantee lowercase English letters only and a sentence length greater than 0.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, String.

Companies

Asked at: Vanguard.