/**
 * 
 */
package cs2020;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author vishnu
 *
 */
public class PremPS5 {

	/**
	 * 
	 */
	public PremPS5() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		StopWatch sw = new StopWatch();
		sw.start();
		HashMap<Integer, Pair> hm = new HashMap<Integer, Pair>();
		try {
			FileReader dataFile = new FileReader(args[0]);
	        BufferedReader bufferedDataFile = new BufferedReader(dataFile);
	        bufferedDataFile.readLine(); // discarding size
	        
	        String line;
	        
	        while ((line = bufferedDataFile.readLine()) != null) {
	        	String sLine = stringSort(line);
	        	Pair p;
	        	p = hm.get(sLine.length());
	        	if(p==null) {
	        		p = new Pair(new HashSet<String>(), 0);
	        		hm.put(sLine.length(), p);
	        	}
	        	
	        	if(p.hs.contains(sLine)) p.count++;
	        	else p.hs.add(sLine);
			}
	        int count = 0;
	        for(Pair p : hm.values()) {
	        	if(p.count==0) continue;
	        	count += (p.count*(p.count+1))/2;
	        }
	        sw.stop();
	        System.out.println(count + " " + sw.getTime());
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	private static String stringSort(String str) {
		char[] a = str.toCharArray();
		Arrays.sort(a);
		return new String(a);
	}
}

class Pair {
	HashSet<String> hs; int count;
	public Pair(HashSet<String> hs, int count) {
		this.hs = hs;
		this.count = count;
	}
}

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(String o1, String o2) {
		if(o1.length() < o2.length()) return -1;
		else if(o1.length() > o2.length()) return 1;
		return 0;
	}
	
}
@@ -794,24 +794,12 @@
 e = 
-stringSort(
 line
-)
 ;%0A%09 
@@ -918,16 +918,26 @@
 t%3CString
+Comparator
 %3E(), 0);
@@ -1029,15 +1029,37 @@
 ins(
+new StringComparator(
 sLine))
+)
  p.c
@@ -1090,22 +1090,44 @@
 .hs.add(
+new StringComparator(
 sLine)
+)
 ;%0A%09%09%09%7D%0A%09
@@ -1416,135 +1416,8 @@
 %0A%09%7D%0A
-%09%0A%09private static String stringSort(String str) %7B%0A%09%09char%5B%5D a = str.toCharArray();%0A%09%09Arrays.sort(a);%0A%09%09return new String(a);%0A%09%7D%0A
 %7D%0A%0Ac
@@ -1439,24 +1439,34 @@
 shSet%3CString
+Comparator
 %3E hs; int co
@@ -1497,16 +1497,26 @@
 t%3CString
+Comparator
 %3E hs, in
@@ -1570,16 +1570,126 @@
 ;%0A%09%7D%0A%7D%0A%0A
+class StringComparator %7B%0A%09String str;%0A%09public StringComparator(String str) %7B%0A%09%09this.str = stringSort(str);%0A%09%7D%0A
 %09/* (non
@@ -1711,37 +1711,8 @@
 see 
-java.util.Comparator#compare(
 java
@@ -1723,18 +1723,24 @@
 g.Object
-, 
+#equals(
 java.lan
@@ -1777,148 +1777,342 @@
 lic 
-int compare(String o1, String o2) %7B%0A%09%09if(o1.length() %3C o2.length()) return -1;%0A%09%09else if(o1.length() %3E o2.length()) return 1;%0A%09%09return 0
+boolean equals(Object obj) %7B%0A%09%09StringComparator sc = (StringComparator) obj;%0A%09%09return str.equals(sc.str);%0A%09%7D%0A%09%0A%09@Override%0A%09public int hashCode() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09return str.hashCode();%0A%09%7D%0A%09private static String stringSort(String str) %7B%0A%09%09char%5B%5D a = str.toCharArray();%0A%09%09Arrays.sort(a);%0A%09%09return new String(a)
 ;%0A%09%7D%0A
-%09%0A
 %7D

Stringcomparator method