3137. Minimum Number of Operations to Make Word K-Periodic Medium
1/**
2 * [3137] Minimum Number of Operations to Make Word K-Periodic
3 *
4 * You are given a string word of size n, and an integer k such that k divides n.
5 * In one operation, you can pick any two indices i and j, that are divisible by k, then replace the <span data-keyword="substring">substring</span> of length k starting at i with the substring of length k starting at j. That is, replace the substring word[i..i + k - 1] with the substring word[j..j + k - 1].<!-- notionvc: 49ac84f7-0724-452a-ab43-0c5e53f1db33 -->
6 * Return the minimum number of operations required to make word k-periodic.
7 * We say that word is k-periodic if there is some string s of length k such that word can be obtained by concatenating s an arbitrary number of times. For example, if word == “ababab”, then word is 2-periodic for s = "ab".
8 *
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io" style="
12 * font-family: Menlo,sans-serif;
13 * font-size: 0.85rem;
14 * ">word = "leetcodeleet", k = 4</span>
15 * Output: <span class="example-io" style="
16 * font-family: Menlo,sans-serif;
17 * font-size: 0.85rem;
18 * ">1</span>
19 * Explanation:
20 * We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
21 * </div>
22 * <strong class="example">Example 2:
23 * <div class="example-block">
24 * Input: <span class="example-io" style="
25 * font-family: Menlo,sans-serif;
26 * font-size: 0.85rem;
27 * ">word = "</span>leetcoleet<span class="example-io" style="
28 * font-family: Menlo,sans-serif;
29 * font-size: 0.85rem;
30 * ">", k = 2</span>
31 * Output: 3
32 * Explanation:
33 * We can obtain a 2-periodic string by applying the operations in the table below.
34 * <table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" height="146" style="border-collapse:collapse; text-align: center; vertical-align: middle;">
35 * <tbody>
36 * <tr>
37 * <th>i</th>
38 * <th>j</th>
39 * <th>word</th>
40 * </tr>
41 * <tr>
42 * <td style="padding: 5px 15px;">0</td>
43 * <td style="padding: 5px 15px;">2</td>
44 * <td style="padding: 5px 15px;">etetcoleet</td>
45 * </tr>
46 * <tr>
47 * <td style="padding: 5px 15px;">4</td>
48 * <td style="padding: 5px 15px;">0</td>
49 * <td style="padding: 5px 15px;">etetetleet</td>
50 * </tr>
51 * <tr>
52 * <td style="padding: 5px 15px;">6</td>
53 * <td style="padding: 5px 15px;">0</td>
54 * <td style="padding: 5px 15px;">etetetetet</td>
55 * </tr>
56 * </tbody>
57 * </table>
58 * </div>
59 * <div id="gtx-trans" style="position: absolute; left: 107px; top: 238.5px;">
60 * <div class="gtx-trans-icon"> </div>
61 * </div>
62 *
63 * Constraints:
64 *
65 * 1 <= n == word.length <= 10^5
66 * 1 <= k <= word.length
67 * k divides word.length.
68 * word consists only of lowercase English letters.
69 *
70 */
71pub struct Solution {}
72
73// problem: https://leetcode.com/problems/minimum-number-of-operations-to-make-word-k-periodic/
74// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-make-word-k-periodic/discuss/?currentPage=1&orderBy=most_votes&query=
75
76// submission codes start here
77
78impl Solution {
79 pub fn minimum_operations_to_make_k_periodic(word: String, k: i32) -> i32 {
80 0
81 }
82}
83
84// submission codes end
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_3137() {
92 }
93}
94
Back
© 2025 bowen.ge All Rights Reserved.