Skip to content

1832. Check if the Sentence Is Pangram 👍

Approach 1: Set

  • Time: $O(n)$
  • Space: $O(26) = O(1)$
1
2
3
4
5
6
class Solution {
 public:
  bool checkIfPangram(string sentence) {
    return unordered_set(sentence.begin(), sentence.end()).size() == 26;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public boolean checkIfPangram(String sentence) {
    Set<Character> seen = new HashSet<>();

    for (final char c : sentence.toCharArray())
      seen.add(c);

    return seen.size() == 26;
  }
}
1
2
3
class Solution:
  def checkIfPangram(self, sentence: str) -> bool:
    return len(set(sentence)) == 26

Approach 2: Bit

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  bool checkIfPangram(string sentence) {
    int seen = 0;

    for (const char c : sentence)
      seen |= 1 << c - 'a';

    return seen == (1 << 26) - 1;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public boolean checkIfPangram(String sentence) {
    int seen = 0;

    for (final char c : sentence.toCharArray())
      seen |= 1 << c - 'a';

    return seen == (1 << 26) - 1;
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def checkIfPangram(self, sentence: str) -> bool:
    seen = 0

    for c in sentence:
      seen |= 1 << string.ascii_lowercase.index(c)

    return seen == (1 << 26) - 1