Skip to main content
Back to problems
Leetcode
Medium
Arrays
Strings
Hash Maps
Minimum Number Of Operations To Make Word K Periodic

Find the minimum number of character changes needed so a word becomes k-periodic.

Acceptance 0%
Problem Statement

Given a lowercase word and an integer kk, make the word k-periodic by changing as few characters as possible.

A word is k-periodic if every character at positions that differ by a multiple of kk is the same. Equivalently, for each residue class modulo kk, all characters in those positions must match.

Your task is to compute the minimum number of operations required, where one operation changes a single character to any other lowercase letter.

Input Format

  • A string word consisting of lowercase English letters.
  • An integer k.

You may assume the inputs describe a valid instance of the problem.

Output Format

  • Return the minimum number of single-character changes needed to make word k-periodic.

Constraints

  • 1 <= k <= len(word)
  • word contains only lowercase English letters
  • One operation changes exactly one character
Examples
Sample cases returned by the problem API.

Example 1

Input

word = "leetcode", k = 2

Output

4

Explanation

Positions 0,2,4,6 are l,e,c,d and positions 1,3,5,7 are e,t,o,e. In each group, keep the most frequent letter and change the others. The minimum total changes is 4.

Example 2

Input

word = "aaabbb", k = 3

Output

0

Explanation

Positions grouped by modulo 3 are a,b, a,b, a,b. Each group already has matching characters after considering the repeating pattern, so no changes are needed.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.