LeetCode 题解工作台
判断句子是否为全字母句
全字母句 指包含英语字母表中每个字母至少一次的句子。 给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。 如果是,返回 true ;否则,返回 false 。 示例 1: 输入: sentence = "thequickbrownfoxjump…
2
题型
7
代码语言
3
相关题
当前训练重点
简单 · 哈希·表·结合·string
答案摘要
遍历字符串 `sentence`,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 个字母即可。 时间复杂度 ,空间复杂度 。其中 为字符串 `sentence` 的长度,而 为字符集大小。本题中 $C = 26$。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 哈希·表·结合·string 题型思路
题目描述
全字母句 指包含英语字母表中每个字母至少一次的句子。
给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。
如果是,返回 true ;否则,返回 false 。
示例 1:
输入:sentence = "thequickbrownfoxjumpsoverthelazydog"
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。
示例 2:
输入:sentence = "leetcode" 输出:false
提示:
1 <= sentence.length <= 1000sentence由小写英语字母组成
解题思路
方法一:数组或哈希表
遍历字符串 sentence,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 个字母即可。
时间复杂度 ,空间复杂度 。其中 为字符串 sentence 的长度,而 为字符集大小。本题中 。
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
return len(set(sentence)) == 26
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) where n is the length of the string, as each character is processed once. Space complexity can be O(1) with a fixed-size array or bitmask, or O(26) for a hash set, which is effectively constant. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Checking if the candidate correctly identifies the pattern as hash table plus string.
- question_mark
Looking for early exit strategies when all letters are found.
- question_mark
Observing how the candidate handles space-efficient tracking using boolean arrays or bitmasks.
常见陷阱
外企场景- error
Forgetting that only lowercase letters are present and using an oversized array.
- error
Not accounting for repeated letters, leading to incorrect counting logic.
- error
Iterating the array or set after every insertion instead of checking only at the end or when full.
进阶变体
外企场景- arrow_right_alt
Determine if a string contains all vowels using a similar hash table approach.
- arrow_right_alt
Check if a string contains all letters from a custom alphabet pattern.
- arrow_right_alt
Verify pangram status ignoring non-letter characters in a mixed string.