LeetCode 题解工作台

判断句子是否为全字母句

全字母句 指包含英语字母表中每个字母至少一次的句子。 给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。 如果是,返回 true ;否则,返回 false 。 示例 1: 输入: sentence = "thequickbrownfoxjump…

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 哈希·表·结合·string

bolt

答案摘要

遍历字符串 `sentence`,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 个字母即可。 时间复杂度 ,空间复杂度 。其中 为字符串 `sentence` 的长度,而 为字符集大小。本题中 $C = 26$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 哈希·表·结合·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

全字母句 指包含英语字母表中每个字母至少一次的句子。

给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句

如果是,返回 true ;否则,返回 false

 

示例 1:

输入:sentence = "thequickbrownfoxjumpsoverthelazydog"
输出:true
解释:sentence 包含英语字母表中每个字母至少一次。

示例 2:

输入:sentence = "leetcode"
输出:false

 

提示:

  • 1 <= sentence.length <= 1000
  • sentence 由小写英语字母组成
lightbulb

解题思路

方法一:数组或哈希表

遍历字符串 sentence,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 2626 个字母即可。

时间复杂度 O(n)O(n),空间复杂度 O(C)O(C)。其中 nn 为字符串 sentence 的长度,而 CC 为字符集大小。本题中 C=26C = 26

1
2
3
4
class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

判断句子是否为全字母句题解:哈希·表·结合·string | LeetCode #1832 简单