LeetCode 题解工作台
情感丰富的文字
有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo" , "hi" -> "hiii" 。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。 对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组…
3
题型
4
代码语言
3
相关题
当前训练重点
中等 · 双·指针·invariant
答案摘要
我们可以遍历数组 ,对于数组中的每个单词 ,判断 是否可以通过扩张得到 ,如果可以,那么答案加一。 因此,问题的关键在于判断单词 是否可以通过扩张得到 。这里我们通过一个 $\textit{check}(s, t)$ 函数来判断。函数的具体实现逻辑如下:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 双·指针·invariant 题型思路
题目描述
有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo", "hi" -> "hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。
对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组扩张从而使其和 S 相同,我们将这个单词定义为可扩张的(stretchy)。扩张操作定义如下:选择一个字母组(包含字母 c ),然后往其中添加相同的字母 c 使其长度达到 3 或以上。
例如,以 "hello" 为例,我们可以对字母组 "o" 扩张得到 "hellooo",但是无法以同样的方法得到 "helloo" 因为字母组 "oo" 长度小于 3。此外,我们可以进行另一种扩张 "ll" -> "lllll" 以获得 "helllllooo"。如果 s = "helllllooo",那么查询词 "hello" 是可扩张的,因为可以对它执行这两种扩张操作使得 query = "hello" -> "hellooo" -> "helllllooo" = s。
输入一组查询单词,输出其中可扩张的单词数量。
示例:
输入: s = "heeellooo" words = ["hello", "hi", "helo"] 输出:1 解释: 我们能通过扩张 "hello" 的 "e" 和 "o" 来得到 "heeellooo"。 我们不能通过扩张 "helo" 来得到 "heeellooo" 因为 "ll" 的长度小于 3 。
提示:
1 <= s.length, words.length <= 1001 <= words[i].length <= 100- s 和所有在
words中的单词都只由小写字母组成。
解题思路
方法一:遍历计数 + 双指针
我们可以遍历数组 ,对于数组中的每个单词 ,判断 是否可以通过扩张得到 ,如果可以,那么答案加一。
因此,问题的关键在于判断单词 是否可以通过扩张得到 。这里我们通过一个 函数来判断。函数的具体实现逻辑如下:
首先判断 和 的长度关系,如果 的长度大于 ,直接返回 ;否则,我们用双指针 和 分别指向 和 ,初始时 和 的值均为 。
如果 和 指向的字符不同,那么 无法通过扩张得到 ,直接返回 ;否则,我们需要判断 指向的字符的连续出现次数 和 指向的字符的连续出现次数 的关系。如果 或者 并且 ,那么 无法通过扩张得到 ,直接返回 ;否则,将 和 分别右移 和 次。继续判断。
如果 和 都到达了字符串的末尾,那么 可以通过扩张得到 ,返回 ,否则返回 。
时间复杂度 ,其中 和 分别为字符串 和数组 的长度,而 为数组 中第 个单词的长度。
class Solution:
def expressiveWords(self, s: str, words: List[str]) -> int:
def check(s, t):
m, n = len(s), len(t)
if n > m:
return False
i = j = 0
while i < m and j < n:
if s[i] != t[j]:
return False
k = i
while k < m and s[k] == s[i]:
k += 1
c1 = k - i
i, k = k, j
while k < n and t[k] == t[j]:
k += 1
c2 = k - j
j = k
if c1 < c2 or (c1 < 3 and c1 != c2):
return False
return i == m and j == n
return sum(check(s, t) for t in words)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on the number of words and the length of each word. The two-pointer approach ensures that each character is processed only once, making the overall time complexity O(n * m), where n is the number of words and m is the average length of the words. Space complexity is O(1) as no extra space is required aside from the input and output. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Ability to implement two-pointer scanning effectively.
- question_mark
Comfort with tracking invariants across two strings during comparison.
- question_mark
Understanding of edge cases and how they affect the algorithm.
常见陷阱
外企场景- error
Not correctly handling edge cases where groups of characters do not meet the required size for extension.
- error
Incorrectly tracking character groups, leading to false negatives for valid words.
- error
Confusing the requirement for extensions with simple character repetition without extension.
进阶变体
外企场景- arrow_right_alt
Extend the problem to handle case-insensitive strings or strings with mixed characters.
- arrow_right_alt
Introduce more complex constraints such as words with non-alphabetical characters.
- arrow_right_alt
Change the minimum group extension requirement to a different threshold, such as 2 or 4 characters.