LeetCode 题解工作台
句子中的有效单词数
句子仅由小写字母( 'a' 到 'z' )、数字( '0' 到 '9' )、连字符( '-' )、标点符号( '!' 、 '.' 和 ',' )以及空格( ' ' )组成。每个句子可以根据空格分解成 一个或者多个 token ,这些 token 之间由一个或者多个空格 ' ' 分隔。 如果一个 to…
1
题型
5
代码语言
3
相关题
当前训练重点
简单 · String-driven solution strategy
答案摘要
我们首先将句子按空格分割成单词,然后对每个单词进行检查,判断是否为有效单词。 对于每个单词,我们可以使用一个布尔变量 来记录是否已经出现过连字符,然后遍历单词中的每个字符,根据题目描述的规则进行判断。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路
题目描述
句子仅由小写字母('a' 到 'z')、数字('0' 到 '9')、连字符('-')、标点符号('!'、'.' 和 ',')以及空格(' ')组成。每个句子可以根据空格分解成 一个或者多个 token ,这些 token 之间由一个或者多个空格 ' ' 分隔。
如果一个 token 同时满足下述条件,则认为这个 token 是一个有效单词:
- 仅由小写字母、连字符和/或标点(不含数字)组成。
- 至多一个 连字符
'-'。如果存在,连字符两侧应当都存在小写字母("a-b"是一个有效单词,但"-ab"和"ab-"不是有效单词)。 - 至多一个 标点符号。如果存在,标点符号应当位于 token 的 末尾 。
这里给出几个有效单词的例子:"a-b."、"afad"、"ba-c"、"a!" 和 "!" 。
给你一个字符串 sentence ,请你找出并返回 sentence 中 有效单词的数目 。
示例 1:
输入:sentence = "cat and dog" 输出:3 解释:句子中的有效单词是 "cat"、"and" 和 "dog"
示例 2:
输入:sentence = "!this 1-s b8d!" 输出:0 解释:句子中没有有效单词 "!this" 不是有效单词,因为它以一个标点开头 "1-s" 和 "b8d" 也不是有效单词,因为它们都包含数字
示例 3:
输入:sentence = "alice and bob are playing stone-game10" 输出:5 解释:句子中的有效单词是 "alice"、"and"、"bob"、"are" 和 "playing" "stone-game10" 不是有效单词,因为它含有数字
提示:
1 <= sentence.length <= 1000sentence由小写英文字母、数字(0-9)、以及字符(' '、'-'、'!'、'.'和',')组成- 句子中至少有
1个 token
解题思路
方法一:模拟
我们首先将句子按空格分割成单词,然后对每个单词进行检查,判断是否为有效单词。
对于每个单词,我们可以使用一个布尔变量 来记录是否已经出现过连字符,然后遍历单词中的每个字符,根据题目描述的规则进行判断。
对于每个字符 ,我们有以下几种情况:
- 如果 是数字,那么 不是有效单词,直接返回 ;
- 如果 是标点符号('!'、'.'、',')且 ,那么 不是有效单词,直接返回 ;
- 如果 是连字符,那么我们需要判断是否满足以下条件:
- 连字符只能出现一次;
- 连字符不能出现在单词的开头或结尾;
- 连字符两侧必须是字母;
- 如果 是字母,那么我们不需要做任何处理。
最后,我们统计出句子中的有效单词数即可。
时间复杂度 ,空间复杂度 。其中 是句子的长度。
class Solution:
def countValidWords(self, sentence: str) -> int:
def check(s: str) -> bool:
st = False
for i, c in enumerate(s):
if c.isdigit() or (c in "!.," and i < len(s) - 1):
return False
if c == "-":
if (
st
or i in (0, len(s) - 1)
or not s[i - 1].isalpha()
or not s[i + 1].isalpha()
):
return False
st = True
return True
return sum(check(s) for s in sentence.split())
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) where n is the sentence length, as each character is inspected once. Space complexity is O(n) due to storing tokens during splitting and temporary variables for validation. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for whether the candidate efficiently splits the sentence without extra string rebuilds.
- question_mark
Check if they correctly enforce hyphen and punctuation placement rules per token.
- question_mark
Notice if the candidate avoids counting empty tokens caused by multiple spaces.
常见陷阱
外企场景- error
Failing to ignore multiple consecutive spaces leads to miscounting empty tokens as invalid words.
- error
Allowing digits or misplaced hyphens within tokens, which violates the problem's strict word definition.
- error
Placing punctuation anywhere except the end of a token and still counting it as valid.
进阶变体
外企场景- arrow_right_alt
Count valid words ignoring case sensitivity or extended Unicode characters.
- arrow_right_alt
Return the list of valid words instead of just the count.
- arrow_right_alt
Validate words with multiple allowed punctuation marks or special symbols.