Character Alternations
Difficulty: Hard

On the long long road to Mordor, Sam and Frodo get to talking...

**Sam:** “Mr. Frodo, I am quite exasperated with our situation.”

**Frodo:** “Sam, that is an interesting choice of words. Note that in the word ‘exasperated’ the letters ‘e’ and ‘a’ alternate 5 times.”  

    exasperated  
    ↑ ↑  ↑ ↑ ↑

**Sam:** “Dear Mr. Frodo. We are starving and being hunted by nasty orcs and giant spiders, I find your sudden interest in spelling most counterintuitive."

**Frodo:**“My goodness Sam! You just topped yourself. The word "counterintuitive" has fully 6 alternations, involving the letters ‘t’ and ‘i’!”

    counterintuitive
        ↑  ↑ ↑ ↑↑↑
        
**Sam:** “Master Frodo, we are starving. I want to go home! Please stop talking about such insignificant matters, or I will surely turn around and quit this instant.”

**Frodo:** “Oh my goodness Sam, you just did it again. The word ‘insignificant’ also has 6 alternations!”  

    insignificant
    ↑↑ ↑ ↑  ↑  ↑

**Sam:** “That does it. I’m heading back to the shire. You can keep the blasted ring.”

**Smeagol:** “Sssilly hobbitssess.”

Frodo loves to find alternating patterns of letters in words. An alternation is defined to be two
distinct characters (letters, digits, or punctuation) that alternate at least three times as the word is read from left to right, as shown in the above examples. Thus, the word “boo” has no alternations, but “booboo” has “bobo” as an alternation of length 4. Upper and lower case characters are considered to be different, and so, “aragorn” contains the 4-element alternation “arar”, but “Aragorn” only has the 3-element alternation “rar”. Digits and punctuation may be used, so “a2b-c2d-” has the alternation “2-2-”.  

The objective of this problem is to write a program to find the length of the longest alternation in a word.

**Input Format:**
Your program will read in a series of strings, one per line. Each string will contain no embedded spaces,but may contain punctuation symbols and digits. *End of input strings is marked by a 0 in a new line.*

**Output Format:**
Your program will output the word and the length of the longest alternation. (It does not need to
output the actual letters.) If there is no alternation of length 3 or more, then it outputs 0 as the length. See the example below.

**Example:**

	Input: 							Output:
    ------------------------------------------------------------
	exasperated 			exasperated has 5 alternations
	counterintuitive 		counterintuitive has 6 alternations
	insignificant 			insignificant has 6 alternations
	Aragorn 				Aragorn has 3 alternations
	a2b-c2d- 				a2b-c2d- has 4 alternations
	cat 					cat has 0 alternations
	ca 						ca has 0 alternations
	c 						c has 0 alternations
    0

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
	import java.io.*;
	import java.util.StringTokenizer;

	/**
 	* The program inputs a string and outputs the maximum number of
 	* alternations of two distinct characters in the string.  For example,
 	* the string "international" has 5 alternations involving the letters
 	* n and t:
 	*
 	*             i n t e r n a t i o n a l
 	*               ^ ^     ^   ^     ^
	 *
	 * Case is significant (so "AbaB" has no alternations), and any
	 * character may be used in an alternation (so "ab1f-gh1-cd1" has
	 * the alternating sequence "1-1-1" of length 5).
	 *
	 * The smallest sized alternation is of length 3 (e.g., "..a..b..a..").
	 * Sequences of 2 or fewer (as in "..a..b..") are not considered to be
	 * alternations, and hence the result is 0 in this case (rather than 2).
	 *
	 * 
	 */
	public class Alternations {

	public static void main(String[] args) {
		try {
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader stdin = new BufferedReader(isr);
			String line = stdin.readLine();
			while (line != null) {
				StringTokenizer tokenizer = new StringTokenizer(line);
												// read first string
				String word = tokenizer.nextToken();
												// convert to char array
				char[] letters = word.toCharArray();
												// get number of alternations
				int nAlts = getMaxAlternations(letters);
				System.out.println(word + " has " + nAlts + " alternations");
				line = stdin.readLine();		// next line
			}
		}
		// Catch input errors
		catch (Exception exception) {
			System.out.println("I/O error.");
		}
	}

	private static int getMaxAlternations(char[] letters) {
		// -------------- ADD YOUR CODE HERE ----------------------
		int maxAlts = 0;						// initially no alternations
		for (int i = 0; i < letters.length; i++) {
			char x = letters[i];				// first character
			for (int j = i+1; j < letters.length; j++) {
				char y = letters[j];			// second character
				if (x != y) {
					int nAlts = getMaxXYAlternations(x, y, letters);
					if (nAlts > maxAlts) maxAlts = nAlts;
				}
			}
		}
		return maxAlts;
	}

	private static int getMaxXYAlternations(char x, char y, char[] letters) {
		char[] theChars = new char[2];			// stogage for the characters
		theChars[0] = x;
		theChars[1] = y;
		int nextMatch = 0;						// start matching with x
		int altCount = 0;						// alternation count
		for (int i = 0; i < letters.length; i++) {
			if (letters[i] == theChars[nextMatch]) {
				altCount++;						// increase count
				nextMatch = 1 - nextMatch;		// go to other character
			}
		}
		return (altCount < 3 ? 0 : altCount);	// return 3 or more
	}
	}