返回介绍

solution / 1800-1899 / 1832.Check if the Sentence Is Pangram / README_EN

发布于 2024-06-17 01:03:14 字数 6075 浏览 0 评论 0 收藏 0

1832. Check if the Sentence Is Pangram

中文文档

Description

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.

Solutions

Solution 1: 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$.

class Solution:
  def checkIfPangram(self, sentence: str) -> bool:
    return len(set(sentence)) == 26
class Solution {
  public boolean checkIfPangram(String sentence) {
    boolean[] vis = new boolean[26];
    for (int i = 0; i < sentence.length(); ++i) {
      vis[sentence.charAt(i) - 'a'] = true;
    }
    for (boolean v : vis) {
      if (!v) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool checkIfPangram(string sentence) {
    int vis[26] = {0};
    for (char& c : sentence) vis[c - 'a'] = 1;
    for (int& v : vis)
      if (!v) return false;
    return true;
  }
};
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
}
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);
}
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)
  }
}
bool checkIfPangram(char* sentence) {
  int vis[26] = {0};
  for (int i = 0; sentence[i]; i++) {
    vis[sentence[i] - 'a'] = 1;
  }
  for (int i = 0; i < 26; i++) {
    if (!vis[i]) {
      return 0;
    }
  }
  return 1;
}

Solution 2: 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$ $1$s 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)$.

class Solution:
  def checkIfPangram(self, sentence: str) -> bool:
    mask = 0
    for c in sentence:
      mask |= 1 << (ord(c) - ord('a'))
    return mask == (1 << 26) - 1
class Solution {
  public boolean checkIfPangram(String sentence) {
    int mask = 0;
    for (int i = 0; i < sentence.length(); ++i) {
      mask |= 1 << (sentence.charAt(i) - 'a');
    }
    return mask == (1 << 26) - 1;
  }
}
class Solution {
public:
  bool checkIfPangram(string sentence) {
    int mask = 0;
    for (char& c : sentence) mask |= 1 << (c - 'a');
    return mask == (1 << 26) - 1;
  }
};
func checkIfPangram(sentence string) bool {
  mask := 0
  for _, c := range sentence {
    mask |= 1 << int(c-'a')
  }
  return mask == 1<<26-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;
}
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
  }
}
bool checkIfPangram(char* sentence) {
  int mark = 0;
  for (int i = 0; sentence[i]; i++) {
    mark |= 1 << (sentence[i] - 'a');
  }
  return mark == (1 << 26) - 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文