// Arup Guha
// 8/15/2019
// Solution for 2018 AP FR Question #2: WordPairList

import java.util.*;

public class WordPairList {

	private ArrayList<WordPair> allPairs;
	
	public WordPairList(String[] words) {
		allPairs = new ArrayList<WordPair>();
		
		// Just double loop and add each appropriate pair.
		for (int i=0; i<words.length; i++)
			for (int j=i+1; j<words.length; j++)
				allPairs.add(new WordPair(words[i], words[j]));
	}
	
	public int numMatches() {
		int res = 0;
		
		// Just go through all pairs and compare the two words in the pair.
		// Honestly, it would be better design if the WordPair class had some
		// method called same() which returned true iff its two words were the same.
		for (WordPair p: allPairs)
			if (p.getFirst().equals(p.getSecond()))
				res++;
		return res;
	}
	
	// Just for testing.
	public String toString() {
		String res = "";
		for (WordPair p: allPairs)
			res = res + p + "\n";
		return res;
	}
	
	// A simple test but added more than 2 copies of a string as it's an important case.
	public static void main(String[] args) {
		String[] stuff = {"hi", "bye", "a", "a", "hi", "hi", "hi", "low", "q", "q"};
		WordPairList wplist = new WordPairList(stuff);
		System.out.println(wplist);
		System.out.println("# of matches = "+wplist.numMatches());
		
		/*** An faster way to solve this problem without objects is as follows:
		     assumes input less than 30000 strings, so no overflow.
		Arrays.sort(stuff);
		int i=0, res = 0;
		while (i < stuff.length) {
			int j = i;
			while (j<stuff.length && stuff[j].equals(stuff[i])) j++;
			res += ((j-i)*(j-i-1)/2);
			i = j;
		}
		System.out.println(res);
		***/
	}
}