TopcoderArchive
Archive
Algorithm problem

TripleString

Problem statement, definition, constraints, and public examples.

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

  1. "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'.
  2. "abbabbabb"
    Returns: 0
    "abbabbabb" is already a triple string.
  3. "ababac"
    Returns: 1
    Changing the last character to 'b' is enough.
  4. "abccaacdbadc"
    Returns: 4
  5. "abcdefghi"
    Returns: 6
Back to all problems