LeetCode 题解工作台

情感丰富的文字

有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo" , "hi" -> "hiii" 。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。 对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组…

category

3

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 双·指针·invariant

bolt

答案摘要

我们可以遍历数组 ,对于数组中的每个单词 ,判断 是否可以通过扩张得到 ,如果可以,那么答案加一。 因此,问题的关键在于判断单词 是否可以通过扩张得到 。这里我们通过一个 $\textit{check}(s, t)$ 函数来判断。函数的具体实现逻辑如下:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 双·指针·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

有时候人们会用重复写一些字母来表示额外的感受,比如 "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 <= 100
  • 1 <= words[i].length <= 100
  • s 和所有在 words 中的单词都只由小写字母组成。
lightbulb

解题思路

方法一:遍历计数 + 双指针

我们可以遍历数组 words\textit{words},对于数组中的每个单词 tt,判断 tt 是否可以通过扩张得到 ss,如果可以,那么答案加一。

因此,问题的关键在于判断单词 tt 是否可以通过扩张得到 ss。这里我们通过一个 check(s,t)\textit{check}(s, t) 函数来判断。函数的具体实现逻辑如下:

首先判断 sstt 的长度关系,如果 tt 的长度大于 ss,直接返回 false\textit{false};否则,我们用双指针 iijj 分别指向 sstt,初始时 iijj 的值均为 00

如果 iijj 指向的字符不同,那么 tt 无法通过扩张得到 ss,直接返回 false\textit{false};否则,我们需要判断 ii 指向的字符的连续出现次数 c1c_1jj 指向的字符的连续出现次数 c2c_2 的关系。如果 c1<c2c_1 \lt c_2 或者 c1<3c_1 \lt 3 并且 c1c2c_1 \neq c_2,那么 tt 无法通过扩张得到 ss,直接返回 false\textit{false};否则,将 iijj 分别右移 c1c_1c2c_2 次。继续判断。

如果 iijj 都到达了字符串的末尾,那么 tt 可以通过扩张得到 ss,返回 true\textit{true},否则返回 false\textit{false}

时间复杂度 O(n×m+i=0m1wi)O(n \times m + \sum_{i=0}^{m-1} w_i),其中 nnmm 分别为字符串 ss 和数组 words\textit{words} 的长度,而 wiw_i 为数组 words\textit{words} 中第 ii 个单词的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)
speed

复杂度分析

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

面试官常问的追问

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

warning

常见陷阱

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

swap_horiz

进阶变体

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

help

常见问题

外企场景

情感丰富的文字题解:双·指针·invariant | LeetCode #809 中等