Question
Minimum Edits for a Repeating Password Pattern
A security system requires passwords to follow a repeating pattern with some period
You are given a lowercase string
Input
The first line contains the string KaTeX can only parse string typed expression.
The second line contains a single integer KaTeX can only parse string typed expression — the target period.
Output
Print a single integer — the minimum number of character changes needed.
Example
Example 1:
Input
The string already satisfies the period-3 property: position 0 matches position 3 ('a'='a'), position 1 matches position 4 ('b'='b'), and position 2 matches position 5 ('c'='c'). No changes are needed.
Example 2:
Input
Positions KaTeX can only parse string typed expression (period-2 residue class starting at 0) hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Positions KaTeX can only parse string typed expression hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Total: KaTeX can only parse string typed expression.
Example 3:
Input
A single character trivially satisfies any period, since there is no second position to compare it against.
Input
abcabc
3Output0ExplanationThe string already satisfies the period-3 property: position 0 matches position 3 ('a'='a'), position 1 matches position 4 ('b'='b'), and position 2 matches position 5 ('c'='c'). No changes are needed.
Example 2:
Input
aabbaa
2Output2ExplanationPositions KaTeX can only parse string typed expression (period-2 residue class starting at 0) hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Positions KaTeX can only parse string typed expression hold KaTeX can only parse string typed expression — majority is 'a' (2 out of 3), needing 1 change. Total: KaTeX can only parse string typed expression.
Example 3:
Input
a
1Output0ExplanationA single character trivially satisfies any period, since there is no second position to compare it against.