You're given a string word of length n and an integer k where k divides n evenly. Your goal is to transform the string into a k-periodic string using the minimum number of operations.
What is a k-periodic string? A string is k-periodic if it can be formed by repeating the same substring of length k multiple times. For example, "ababab" is 2-periodic because it repeats "ab" three times.
Operation allowed: You can pick any two indices i and j that are both divisible by k, then replace the substring word[i..i+k-1] with the substring word[j..j+k-1].
Example: If word = "abcdef" and k = 2, you can replace "ab" (at index 0) with "ef" (at index 4) to get "efcdef".
Return the minimum number of operations needed to make the string k-periodic.
Input & Output
Visualization
Time & Space Complexity
Single pass through the string, each substring operation takes O(k) time
Hash map stores at most n/k unique substrings, each of length k
Constraints
- 1 โค word.length โค 105
- 1 โค k โค word.length
- k divides word.length
- word consists only of lowercase English letters