LeetCode 题解工作台

构建回文串检测

给你一个字符串 s ,请你对 s 的子串进行检测。 每次检测,待检子串都可以表示为 queries[i] = [left, right, k] 。我们可以 重新排列 子串 s[left], ..., s[right] ,并从中选择 最多 k 项替换成任何小写英文字母。 如果在上述检测过程中,子串可以…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们先考虑一个子串能否在经过最多 次替换后变成回文串,显然,我们需要统计子串中每个字符出现的次数,这可以通过前缀和来实现。对于出现偶数次的字符,我们不需要进行替换,对于出现奇数次的字符,我们需要进行替换,替换的次数为 $\lfloor \frac{x}{2} \rfloor$,其中 为出现奇数次的字符的个数。如果 $\lfloor \frac{x}{2} \rfloor \leq k$,那么这…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 s,请你对 s 的子串进行检测。

每次检测,待检子串都可以表示为 queries[i] = [left, right, k]。我们可以 重新排列 子串 s[left], ..., s[right],并从中选择 最多 k 项替换成任何小写英文字母。 

如果在上述检测过程中,子串可以变成回文形式的字符串,那么检测结果为 true,否则结果为 false

返回答案数组 answer[],其中 answer[i] 是第 i 个待检子串 queries[i] 的检测结果。

注意:在替换时,子串中的每个字母都必须作为 独立的 项进行计数,也就是说,如果 s[left..right] = "aaa" 且 k = 2,我们只能替换其中的两个字母。(另外,任何检测都不会修改原始字符串 s,可以认为每次检测都是独立的)

 

示例:

输入:s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
输出:[true,false,false,true,true]
解释:
queries[0] : 子串 = "d",回文。
queries[1] : 子串 = "bc",不是回文。
queries[2] : 子串 = "abcd",只替换 1 个字符是变不成回文串的。
queries[3] : 子串 = "abcd",可以变成回文的 "abba"。 也可以变成 "baab",先重新排序变成 "bacd",然后把 "cd" 替换为 "ab"。
queries[4] : 子串 = "abcda",可以变成回文的 "abcba"。

 

提示:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s 中只有小写英文字母
lightbulb

解题思路

方法一:前缀和

我们先考虑一个子串能否在经过最多 kk 次替换后变成回文串,显然,我们需要统计子串中每个字符出现的次数,这可以通过前缀和来实现。对于出现偶数次的字符,我们不需要进行替换,对于出现奇数次的字符,我们需要进行替换,替换的次数为 x2\lfloor \frac{x}{2} \rfloor,其中 xx 为出现奇数次的字符的个数。如果 x2k\lfloor \frac{x}{2} \rfloor \leq k,那么这个子串就可以变成回文串。

因此,我们定义一个前缀和数组 ssss,其中 ss[i][j]ss[i][j] 表示字符串 ss 的前 ii 个字符中,字符 jj 出现的次数。那么对于一个子串 s[l..r]s[l..r],我们可以通过 ss[r+1][j]ss[l][j]ss[r + 1][j] - ss[l][j] 得到子串中字符 jj 出现的次数。我们遍历所有的查询,对于每个查询 [l,r,k][l, r, k],我们统计子串 s[l..r]s[l..r] 中出现奇数次的字符的个数 xx,如果 x2k\lfloor \frac{x}{2} \rfloor \leq k,那么这个子串就可以变成回文串。

时间复杂度 O((n+m)×C)O((n + m) \times C),空间复杂度 O(n×C)O(n \times C),其中 nnmm 分别为字符串 ss 和查询数组的长度;而 CC 为字符集的大小,本题中字符集为小写英文字母,因此 C=26C = 26

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def canMakePaliQueries(self, s: str, queries: List[List[int]]) -> List[bool]:
        n = len(s)
        ss = [[0] * 26 for _ in range(n + 1)]
        for i, c in enumerate(s, 1):
            ss[i] = ss[i - 1][:]
            ss[i][ord(c) - ord("a")] += 1
        ans = []
        for l, r, k in queries:
            cnt = sum((ss[r + 1][j] - ss[l][j]) & 1 for j in range(26))
            ans.append(cnt // 2 <= k)
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Assess if the candidate understands the importance of character frequency in palindrome problems.

  • question_mark

    Look for familiarity with efficient substring processing techniques, such as prefix sums or sliding windows.

  • question_mark

    Evaluate if the candidate can optimize the solution for large inputs, given the constraints.

warning

常见陷阱

外企场景
  • error

    Failing to account for character frequencies correctly, especially the condition that at most one character can have an odd frequency.

  • error

    Not considering the effect of `k` properly, which could allow for a substring to become a palindrome if enough modifications are allowed.

  • error

    Overcomplicating the solution with unnecessary string operations or incorrect handling of indices when counting character frequencies.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Extending the problem to handle substrings with multiple occurrences of a character beyond a single odd count.

  • arrow_right_alt

    Optimizing for different kinds of queries, such as range queries with multiple modifications allowed.

  • arrow_right_alt

    Changing the constraint to allow replacements only within a fixed subset of characters, not any lowercase English letter.

help

常见问题

外企场景

构建回文串检测题解:数组·哈希·扫描 | LeetCode #1177 中等