package com.interview.string;

import java.util.*;

/**
 * https://leetcode.com/problems/anagrams/
 */
public class GroupAnagramsTogether {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs == null || strs.length == 0) return new ArrayList<List<String>>();
        Map<String, Map<String, Integer>> anagramGroup = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String sorted = new String(chars);
            if (anagramGroup.containsKey(sorted)) {
                Map<String, Integer> r = anagramGroup.get(sorted);
                if (r.containsKey(str)) {
                    r.put(str, r.get(str) + 1);
                } else {
                    r.put(str, 1);
                }
            } else {
                Map<String, Integer> r = new TreeMap<>();
                r.put(str, 1);
                anagramGroup.put(sorted, r);
            }
        }
        List<List<String>> result = new ArrayList<>();
        for (Map<String, Integer> value : anagramGroup.values()) {
            List<String> r = new ArrayList<>();
            for (String k : value.keySet()) {
                for (int i = 0; i < value.get(k) ; i++) {
                    r.add(k);
                }
            }
            result.add(r);
        }
        return result;
    }
}

@@ -239,16 +239,28 @@
 th == 0)
+%0A           
  return 
@@ -298,27 +298,106 @@
         
-Map%3CString,
+%0A        int listIndex = 0;%0A        List%3CList%3CString%3E%3E result = new ArrayList%3C%3E();%0A       
  Map%3CStr
@@ -409,17 +409,16 @@
 Integer%3E
-%3E
  anagram
@@ -434,32 +434,41 @@
 ew HashMap%3C%3E();%0A
+        %0A
         for (Str
@@ -678,38 +678,25 @@
         
-Map%3CString, Integer%3E r
+int index
  = anagr
@@ -736,159 +736,93 @@
     
-if (r.containsKey(str)) %7B%0A                    r.put(str, r.get(str) + 1);%0A                %7D else %7B%0A                    r.put(str, 1);%0A                %7D
+List%3CString%3E listResult = result.get(index);    %0A                listResult.add(str);
 %0A   
@@ -859,122 +859,86 @@
     
-Map
+List
 %3CString
-, Integer%3E r = new TreeMap%3C%3E();%0A                r.put(str, 1);%0A                anagramGroup.put(sorted, 
+%3E resultList = new ArrayList%3C%3E();%0A                resultList.add(st
 r);%0A
@@ -953,72 +953,44 @@
     
-%7D%0A
     
-    %7D%0A        List%3CList%3CString%3E%3E result = new Array
+result.add(listIndex, result
 List
-%3C%3E(
 );%0A 
@@ -1000,307 +1000,95 @@
     
-for (Map%3CString, Integer%3E value : anagramGroup.values()) %7B%0A            List%3CString%3E r = new ArrayList%3C%3E();%0A            for (String k : value.keySet()) %7B%0A                for (int i = 0; i %3C value.get(k) ; i++) %7B%0A                    r.add(k);%0A                %7D%0A            %7D%0A            result.add(r);
+        anagramGroup.put(sorted, listIndex);%0A                listIndex++;%0A            %7D
 %0A   

improve origin solution