LeetCode 题解工作台

找出出现至少三次的最长特殊子字符串 II

给你一个仅由小写英文字母组成的字符串 s 。 如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd" 、 "zz" 和 "f" 是特殊字符串。 返回在 s 中出现 至少三次 的 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·搜索·答案·空间

bolt

答案摘要

我们注意到,如果一个长度为 且出现至少三次的特殊子字符串存在,那么长度为 的特殊子字符串也一定存在,这存在着单调性,因此我们可以使用二分查找的方法来找到最长的特殊子字符串。 我们定义二分查找的左边界 $l = 0$,右边界 $r = n$,其中 是字符串的长度。每次二分查找的过程中,我们取 $mid = \lfloor \frac{l + r + 1}{2} \rfloor$,如果长度为 …

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个仅由小写英文字母组成的字符串 s

如果一个字符串仅由单一字符组成,那么它被称为 特殊 字符串。例如,字符串 "abc" 不是特殊字符串,而字符串 "ddd""zz""f" 是特殊字符串。

返回在 s 中出现 至少三次 最长特殊子字符串 的长度,如果不存在出现至少三次的特殊子字符串,则返回 -1

子字符串 是字符串中的一个连续 非空 字符序列。

 

示例 1:

输入:s = "aaaa"
输出:2
解释:出现三次的最长特殊子字符串是 "aa" :子字符串 "aaaa"、"aaaa" 和 "aaaa"。
可以证明最大长度是 2 。

示例 2:

输入:s = "abcdef"
输出:-1
解释:不存在出现至少三次的特殊子字符串。因此返回 -1 。

示例 3:

输入:s = "abcaba"
输出:1
解释:出现三次的最长特殊子字符串是 "a" :子字符串 "abcaba"、"abcaba" 和 "abcaba"。
可以证明最大长度是 1 。

 

提示:

  • 3 <= s.length <= 5 * 105
  • s 仅由小写英文字母组成。
lightbulb

解题思路

方法一:二分查找 + 滑动窗口计数

我们注意到,如果一个长度为 xx 且出现至少三次的特殊子字符串存在,那么长度为 x1x-1 的特殊子字符串也一定存在,这存在着单调性,因此我们可以使用二分查找的方法来找到最长的特殊子字符串。

我们定义二分查找的左边界 l=0l = 0,右边界 r=nr = n,其中 nn 是字符串的长度。每次二分查找的过程中,我们取 mid=l+r+12mid = \lfloor \frac{l + r + 1}{2} \rfloor,如果长度为 midmid 的特殊子字符串存在,那么我们就将左边界更新为 midmid,否则我们就将右边界更新为 mid1mid - 1。在二分查找的过程中,我们使用滑动窗口来计算特殊子字符串的个数。

具体地,我们设计一个函数 check(x)check(x),表示长度为 xx 且出现至少三次的特殊子字符串是否存在。

在函数 check(x)check(x) 中,我们定义一个哈希表或长度为 2626 的数组 cntcnt,其中 cnt[i]cnt[i] 表示长度为 xx,且由第 ii 个小写字母组成的特殊子字符串的个数。我们遍历字符串 ss,如果当前遍历到的字符为 s[i]s[i],那么我们将指针 jj 向右移动,直到 s[j]s[i]s[j] \neq s[i],此时 s[ij1]s[i \cdots j-1] 就是一个长度为 xx 的特殊子字符串,我们将 cnt[s[i]]cnt[s[i]] 增加 max(0,jix+1)\max(0, j - i - x + 1),然后将指针 ii 更新为 jj

在遍历结束之后,我们遍历数组 cntcnt,如果存在 cnt[i]3cnt[i] \geq 3,那么就说明长度为 xx 且出现至少三次的特殊子字符串存在,我们返回 truetrue,否则返回 falsefalse

时间复杂度 O((n+Σ)×logn)O((n + |\Sigma|) \times \log n),空间复杂度 O(Σ)O(|\Sigma|),其中 nn 是字符串 ss 的长度,而 Σ|\Sigma| 表示字符集的大小,本题中字符集为小写英文字母,因此 Σ=26|\Sigma| = 26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
    def maximumLength(self, s: str) -> int:
        def check(x: int) -> bool:
            cnt = defaultdict(int)
            i = 0
            while i < n:
                j = i + 1
                while j < n and s[j] == s[i]:
                    j += 1
                cnt[s[i]] += max(0, j - i - x + 1)
                i = j
            return max(cnt.values()) >= 3

        n = len(s)
        l, r = 0, n
        while l < r:
            mid = (l + r + 1) >> 1
            if check(mid):
                l = mid
            else:
                r = mid - 1
        return -1 if l == 0 else l
speed

复杂度分析

指标
时间O(n)
空间O(c \cdot k) \approx O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate apply binary search effectively on the valid answer space?

  • question_mark

    Does the candidate optimize the substring check with hash tables and sliding windows?

  • question_mark

    How well does the candidate manage edge cases, such as strings without repeating substrings?

warning

常见陷阱

外企场景
  • error

    Failing to handle edge cases where no special substring occurs three times.

  • error

    Inefficiently checking all substrings without using binary search and hash tables.

  • error

    Overcomplicating the solution with unnecessary algorithms or data structures.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the problem asked for substrings that occur at least twice instead of three times?

  • arrow_right_alt

    What if the substring could be made up of more than one character?

  • arrow_right_alt

    What if the input string contained uppercase letters or was case-insensitive?

help

常见问题

外企场景

找出出现至少三次的最长特殊子字符串 II题解:二分·搜索·答案·空间 | LeetCode #2982 中等