Problem Statement
A string is called a triple if it is a concatenation of three copies of another string. For example, "abbabbabb", "www", "testtesttest" are triple strings, while "aaaa" and "ababac" are not.
You are given a String s. The length of s is divisible by 3. You are allowed to choose any number of letters in s and replace each of them with a different letter. Return the minimal number of letters you must change to transform s into a triple string.
Definition
- Class:
- TripleString
- Method:
- minChanges
- Parameters:
- String
- Returns:
- int
- Method signature:
- int minChanges(String s)
- (be sure your method is public)
Constraints
- s will contain between 3 and 48 characters, inclusive.
- The number of characters in s will be divisible by 3.
- s will contain only lowercase letters ('a'-'z').
Examples
"abcxpcaqc"
Returns: 3
The three parts of the input string are "abc", "xpc" and "aqc". One of the possible solutions is to change 'x' to 'a', 'b' to 'q' and 'p' to 'q'."abbabbabb"
Returns: 0
"abbabbabb" is already a triple string."ababac"
Returns: 1
Changing the last character to 'b' is enough."abccaacdbadc"
Returns: 4
"abcdefghi"
Returns: 6