Class Solution
-
- All Implemented Interfaces:
public final class Solution3137 - Minimum Number of Operations to Make Word K-Periodic.
Medium
You are given a string
wordof sizen, and an integerksuch thatkdividesn.In one operation, you can pick any two indices
iandj, that are divisible byk, then replace the substring of lengthkstarting atiwith the substring of lengthkstarting atj. That is, replace the substringword[i..i + k - 1]with the substringword[j..j + k - 1].Return the minimum number of operations required to make
wordk-periodic.We say that
wordis k-periodic if there is some stringsof lengthksuch thatwordcan be obtained by concatenatingsan arbitrary number of times. For example, ifword == “ababab”, thenwordis 2-periodic fors = "ab".Example 1:
Input: word = "leetcodeleet", k = 4
Output: 1
Explanation:
We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
Example 2:
Input: word = "leetcoleet", k = 2
Output: 3
Explanation:
We can obtain a 2-periodic string by applying the operations in the table below.
i j word 0 2 etetcoleet 4 0 etetetleet 6 0 etetetetetConstraints:
<code>1 <= n == word.length <= 10<sup>5</sup></code>
1 <= k <= word.lengthkdividesword.length.wordconsists only of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminimumOperationsToMakeKPeriodic(String word, Integer k)-
-
Method Detail
-
minimumOperationsToMakeKPeriodic
final Integer minimumOperationsToMakeKPeriodic(String word, Integer k)
-
-
-
-