LeetCode 题解工作台

K 周期字符串需要的最少操作次数

给你一个长度为 n 的字符串 word 和一个整数 k ,其中 k 是 n 的因数。 在一次操作中,你可以选择任意两个下标 i 和 j ,其中 0 ,且这两个下标都可以被 k 整除,然后用从 j 开始的长度为 k 的子串替换从 i 开始的长度为 k 的子串。也就是说,将子串 word[i..i + …

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 哈希·表·结合·string

bolt

答案摘要

我们可以将字符串 `word` 按照长度为 的子串进行分组,然后统计每个子串出现的次数,最后返回 减去出现次数最多的子串的次数即可。 时间复杂度 ,空间复杂度 。其中 为字符串 `word` 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 哈希·表·结合·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个长度为 n 的字符串 word 和一个整数 k ,其中 kn 的因数。

在一次操作中,你可以选择任意两个下标 ij,其中 0 <= i, j < n ,且这两个下标都可以被 k 整除,然后用从 j 开始的长度为 k 的子串替换从 i 开始的长度为 k 的子串。也就是说,将子串 word[i..i + k - 1] 替换为子串 word[j..j + k - 1]

返回使 word 成为 K 周期字符串 所需的 最少 操作次数。

如果存在某个长度为 k 的字符串 s,使得 word 可以表示为任意次数连接 s ,则称字符串 wordK 周期字符串 。例如,如果 word == "ababab",那么 word 就是 s = "ab" 时的 2 周期字符串 。

 

示例 1:

输入:word = "leetcodeleet", k = 4

输出:1

解释:可以选择 i = 4 和 j = 0 获得一个 4 周期字符串。这次操作后,word 变为 "leetleetleet" 。

示例 2:

输入:word = "leetcoleet", k = 2

输出:3

解释:可以执行以下操作获得一个 2 周期字符串。

i j word
0 2 etetcoleet
4 0 etetetleet
6 0 etetetetet

 

提示:

  • 1 <= n == word.length <= 105
  • 1 <= k <= word.length
  • k 能整除 word.length
  • word 仅由小写英文字母组成。
lightbulb

解题思路

方法一:计数

我们可以将字符串 word 按照长度为 kk 的子串进行分组,然后统计每个子串出现的次数,最后返回 n/kn/k 减去出现次数最多的子串的次数即可。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为字符串 word 的长度。

1
2
3
4
5
class Solution:
    def minimumOperationsToMakeKPeriodic(self, word: str, k: int) -> int:
        n = len(word)
        return n // k - max(Counter(word[i : i + k] for i in range(0, n, k)).values())
speed

复杂度分析

指标
时间complexity depends on the final approach, but it generally involves iterating through the string and processing substrings, which gives an overall complexity of O(n). The space complexity is also O(n) due to the storage required for substring frequencies.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for a solution that makes use of hash tables for substring frequency calculation.

  • question_mark

    Focus on the understanding of substring counting and minimizing operations based on frequency analysis.

  • question_mark

    Evaluate how well the candidate can optimize the solution for large inputs by utilizing efficient data structures.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the problem by trying to manipulate the string directly instead of counting substring frequencies.

  • error

    Failing to account for the fact that indices i and j must be divisible by k when performing operations.

  • error

    Not considering edge cases, such as strings where no operations are needed or all characters are already the same.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if k does not divide n? In this case, the problem is invalid, so ensure the constraint k divides n is met.

  • arrow_right_alt

    Consider the case where the string is already k-periodic, and no operations are needed.

  • arrow_right_alt

    Explore variations where the operations involve multiple substrings at once or restricted to certain positions.

help

常见问题

外企场景

K 周期字符串需要的最少操作次数题解:哈希·表·结合·string | LeetCode #3137 中等