LeetCode 题解工作台

最美子字符串的数目

如果某个字符串中 至多一个 字母出现 奇数 次,则称其为 最美 字符串。 例如, "ccjjc" 和 "abab" 都是最美字符串,但 "ab" 不是。 给你一个字符串 word ,该字符串由前十个小写英文字母组成( 'a' 到 'j' )。请你返回 word 中 最美非空子字符串 的数目 。 如果…

category

4

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 前缀和

bolt

答案摘要

由于字符串中只包含 个小写字母,因此可以用一个长度为 的二进制数表示字符串中每个字母的奇偶性,其中第 位为 表示第 个字母出现了奇数次,为 表示第 个字母出现了偶数次。 我们遍历字符串的每个字符,用一个变量 维护当前字符串的前缀异或值,用一个数组 维护每个前缀异或值出现的次数,初始时 $st = 0$, $cnt[0] = 1$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 前缀和 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

如果某个字符串中 至多一个 字母出现 奇数 次,则称其为 最美 字符串。

  • 例如,"ccjjc""abab" 都是最美字符串,但 "ab" 不是。

给你一个字符串 word ,该字符串由前十个小写英文字母组成('a''j')。请你返回 word最美非空子字符串 的数目如果同样的子字符串在 word 中出现多次,那么应当对 每次出现 分别计数

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

 

示例 1:

输入:word = "aba"
输出:4
解释:4 个最美子字符串如下所示:
- "aba" -> "a"
- "aba" -> "b"
- "aba" -> "a"
- "aba" -> "aba"

示例 2:

输入:word = "aabb"
输出:9
解释:9 个最美子字符串如下所示:
- "aabb" -> "a"
- "aabb" -> "aa"
- "aabb" -> "aab"
- "aabb" -> "aabb"
- "aabb" -> "a"
- "aabb" -> "abb"
- "aabb" -> "b"
- "aabb" -> "bb"
- "aabb" -> "b"

示例 3:

输入:word = "he"
输出:2
解释:2 个最美子字符串如下所示:
- "he" -> "h"
- "he" -> "e"

 

提示:

  • 1 <= word.length <= 105
  • word 由从 'a''j' 的小写英文字母组成
lightbulb

解题思路

方法一:前缀异或 + 计数

由于字符串中只包含 1010 个小写字母,因此可以用一个长度为 1010 的二进制数表示字符串中每个字母的奇偶性,其中第 ii 位为 11 表示第 ii 个字母出现了奇数次,为 00 表示第 ii 个字母出现了偶数次。

我们遍历字符串的每个字符,用一个变量 stst 维护当前字符串的前缀异或值,用一个数组 cntcnt 维护每个前缀异或值出现的次数,初始时 st=0st = 0, cnt[0]=1cnt[0] = 1

对于当前遍历到的字符,我们更新其前缀异或值。如果当前的前缀异或值出现了 cnt[st]cnt[st] 次,也就意味着有 cnt[st]cnt[st] 个子字符串满足所有字母的出现次数均为偶数,因此我们将答案增加 cnt[st]cnt[st]。此外,对于 0i<100 \le i < 10,如果当前的前缀异或值 stst 的第 ii 位为 11,那么我们还可以找到一个字母出现了奇数次,我们将答案增加 cnt[st(1<<i)]cnt[st \oplus (1 << i)]。最后,我们将 stst 出现的次数增加 11。继续遍历下一个字符,直到遍历完整个字符串。

时间复杂度 O(n×Σ)O(n \times \Sigma),空间复杂度 O(2Σ)O(2^{\Sigma}),其中 Σ=10\Sigma = 10,而 nn 为字符串的长度。

相似题目:

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def wonderfulSubstrings(self, word: str) -> int:
        cnt = Counter({0: 1})
        ans = st = 0
        for c in word:
            st ^= 1 << (ord(c) - ord("a"))
            ans += cnt[st]
            for i in range(10):
                ans += cnt[st ^ (1 << i)]
            cnt[st] += 1
        return ans
speed

复杂度分析

指标
时间O(NA)
空间O(N)
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate can apply bit manipulation to represent frequencies efficiently.

  • question_mark

    Candidate identifies the need for a hash table to track frequency states over prefixes.

  • question_mark

    Candidate recognizes the importance of reducing the problem complexity with prefix-based analysis.

warning

常见陷阱

外企场景
  • error

    Failing to efficiently represent frequencies with a bitmask, leading to unnecessary complexity.

  • error

    Not considering the need to track multiple occurrences of bitmask states using a hash table.

  • error

    Overcomplicating the solution by checking all substrings individually instead of focusing on prefixes.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Handling a larger set of characters beyond 'a' to 'j'.

  • arrow_right_alt

    Optimizing the algorithm to handle even larger strings with stricter time limits.

  • arrow_right_alt

    Exploring dynamic programming approaches to optimize the bitmask handling.

help

常见问题

外企场景

最美子字符串的数目题解:前缀和 | LeetCode #1915 中等