LeetCode 题解工作台

不同的循环子字符串

给你一个字符串 text ,请你返回满足下述条件的 不同 非空子字符串的数目: 可以写成某个字符串与其自身相连接的形式(即,可以写为 a + a ,其中 a 是某个字符串)。 例如, abcabc 就是 abc 和它自身连接形成的。 示例 1: 输入: text = "abcabcabc" 输出: …

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · string·结合·字典树

bolt

答案摘要

字符串哈希是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 0。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。 取一固定值 BASE,把字符串看作是 BASE 进制数,并分配一个大于 0 的数值,代表每种字符。一般来说,我们分配的数值都远小于 BASE。例如,对于小写字母构成的字符串,可以令 a=1, b=2, ..., z=26。取一固定值 MOD,求出该 BAS…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 string·结合·字典树 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 text ,请你返回满足下述条件的 不同 非空子字符串的数目:

  • 可以写成某个字符串与其自身相连接的形式(即,可以写为 a + a,其中 a 是某个字符串)。

例如,abcabc 就是 abc 和它自身连接形成的。

 

示例 1:

输入:text = "abcabcabc"
输出:3
解释:3 个子字符串分别为 "abcabc","bcabca" 和 "cabcab" 。

示例 2:

输入:text = "leetcodeleetcode"
输出:2
解释:2 个子字符串为 "ee" 和 "leetcodeleetcode" 。

 

提示:

  • 1 <= text.length <= 2000
  • text 只包含小写英文字母。
lightbulb

解题思路

方法一:字符串哈希

字符串哈希是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 0。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。

取一固定值 BASE,把字符串看作是 BASE 进制数,并分配一个大于 0 的数值,代表每种字符。一般来说,我们分配的数值都远小于 BASE。例如,对于小写字母构成的字符串,可以令 a=1, b=2, ..., z=26。取一固定值 MOD,求出该 BASE 进制对 M 的余数,作为该字符串的 hash 值。

一般来说,取 BASE=131 或者 BASE=13331,此时 hash 值产生的冲突概率极低。只要两个字符串 hash 值相同,我们就认为两个字符串是相等的。通常 MOD 取 2^64,C++ 里,可以直接使用 unsigned long long 类型存储这个 hash 值,在计算时不处理算术溢出问题,产生溢出时相当于自动对 2^64 取模,这样可以避免低效取模运算。

除了在极特殊构造的数据上,上述 hash 算法很难产生冲突,一般情况下上述 hash 算法完全可以出现在题目的标准答案中。我们还可以多取一些恰当的 BASE 和 MOD 的值(例如大质数),多进行几组 hash 运算,当结果都相同时才认为原字符串相等,就更加难以构造出使这个 hash 产生错误的数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
    def distinctEchoSubstrings(self, text: str) -> int:
        def get(l, r):
            return (h[r] - h[l - 1] * p[r - l + 1]) % mod

        n = len(text)
        base = 131
        mod = int(1e9) + 7
        h = [0] * (n + 10)
        p = [1] * (n + 10)
        for i, c in enumerate(text):
            t = ord(c) - ord('a') + 1
            h[i + 1] = (h[i] * base) % mod + t
            p[i + 1] = (p[i] * base) % mod
        vis = set()
        for i in range(n - 1):
            for j in range(i + 1, n, 2):
                k = (i + j) >> 1
                a = get(i + 1, k + 1)
                b = get(k + 2, j + 1)
                if a == b:
                    vis.add(a)
        return len(vis)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate should demonstrate a strong grasp of string manipulation techniques like Trie usage and rolling hashes.

  • question_mark

    Look for understanding of how to handle distinct substrings efficiently.

  • question_mark

    Pay attention to how they balance time complexity with space usage, especially in terms of optimizing substring checks.

warning

常见陷阱

外企场景
  • error

    Failing to account for overlapping substrings that are distinct but share characters, which can lead to incorrect results.

  • error

    Not considering edge cases like very short strings or strings with only one character repeated multiple times.

  • error

    Overcomplicating the problem by not optimizing substring checking, leading to poor performance for larger inputs.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider handling strings with uppercase letters or non-alphabetical characters.

  • arrow_right_alt

    Expand to handle substrings that can be written as multiple concatenations of a string, not just two.

  • arrow_right_alt

    Extend the problem to find the longest echo substring instead of just counting them.

help

常见问题

外企场景

不同的循环子字符串题解:string·结合·字典树 | LeetCode #1316 困难