diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md index 194ca963afe19..4e30bc8ad5798 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md @@ -38,27 +38,73 @@
  • sentence 由小写英语字母组成
  • - ## 解法 +转为 Set,判断 Set 长度是否等于 26。若是,说明是全字母句。也可以使用位运算。 + ### **Python3** +集合去重并计数: + ```python +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + return len(set(sentence)) == 26 +``` + +位运算: +```python +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + res = 0 + for c in sentence: + diff = ord(c) - ord('a') + res |= (1 << diff) + if res == 0x3ffffff: + return True + return False ``` ### **Java** +集合去重并计数: + ```java +class Solution { + public boolean checkIfPangram(String sentence) { + Set s = new HashSet<>(); + for (int i = 0; i < sentence.length(); ++i) { + s.add(sentence.charAt(i)); + if (s.size() == 26) return true; + } + return false; + } +} +``` +位运算: + +```java +class Solution { + public boolean checkIfPangram(String sentence) { + int res = 0; + for (int i = 0; i < sentence.length(); ++i) { + int diff = sentence.charAt(i) - 'a'; + res |= (1 << diff); + if (res == 0x3ffffff) return true; + } + return false; + } +} ``` ### **...** diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md index 6471f5a89a65a..dbb22148e6c10 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md @@ -32,21 +32,65 @@
  • sentence consists of lowercase English letters.
  • - ## Solutions ### **Python3** +Set: + ```python +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + return len(set(sentence)) == 26 +``` +Bit Manipulation: + +```python +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + res = 0 + for c in sentence: + diff = ord(c) - ord('a') + res |= (1 << diff) + if res == 0x3ffffff: + return True + return False ``` ### **Java** +HashSet: + ```java +class Solution { + public boolean checkIfPangram(String sentence) { + Set s = new HashSet<>(); + for (int i = 0; i < sentence.length(); ++i) { + s.add(sentence.charAt(i)); + if (s.size() == 26) return true; + } + return false; + } +} +``` +Bit Manipulation: + +```java +class Solution { + public boolean checkIfPangram(String sentence) { + int res = 0; + for (int i = 0; i < sentence.length(); ++i) { + int diff = sentence.charAt(i) - 'a'; + res |= (1 << diff); + if (res == 0x3ffffff) return true; + } + return false; + } +} ``` ### **...** diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.java b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.java new file mode 100644 index 0000000000000..b08ac2c99635f --- /dev/null +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public boolean checkIfPangram(String sentence) { + int res = 0; + for (int i = 0; i < sentence.length(); ++i) { + int diff = sentence.charAt(i) - 'a'; + res |= (1 << diff); + if (res == 0x3ffffff) return true; + } + return false; + } +} \ No newline at end of file diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.py b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.py new file mode 100644 index 0000000000000..095726a601006 --- /dev/null +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def checkIfPangram(self, sentence: str) -> bool: + return len(set(sentence)) == 26