package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * https://leetcode.com/problems/anagrams/
 */
public class Problem49 {
    public List<String> anagrams(String[] strs) {
        class Anagram {
            String sorted;
            String original;

            Anagram(String sorted, String original) {
                this.sorted = sorted;
                this.original = original;
            }
        }

        List<Anagram> sortedStrings = new ArrayList<>();
        for (String s : strs) {
            if (s.isEmpty()) {
                sortedStrings.add(new Anagram(s, s));
            } else {
                char[] chars = s.toCharArray();
                Arrays.sort(chars);
                sortedStrings.add(new Anagram(new String(chars), s));
            }
        }

        List<String> result = new ArrayList<>();
        Set<Integer> ignored = new HashSet<>();
        for (int i = 0; i < sortedStrings.size(); i++) {
            for (int j = 0; j < sortedStrings.size(); j++) {
                if (i == j) {
                    continue;
                }
                if (sortedStrings.get(i).sorted.equals(sortedStrings.get(j).sorted)) {
                    if (!ignored.contains(i)) {
                        result.add(sortedStrings.get(i).original);
                    }
                    ignored.add(i);
                }
            }
        }
        return result;
    }
}

@@ -86,19 +86,19 @@
 til.Hash
-Set
+Map
 ;%0Aimport
@@ -131,19 +131,55 @@
 va.util.
-Set
+Map;%0Aimport java.util.stream.Collectors
 ;%0A%0A/**%0A 
@@ -210,16 +210,22 @@
 roblems/
+group-
 anagrams
@@ -266,16 +266,21 @@
  public 
+List%3C
 List%3CStr
@@ -283,16 +283,17 @@
 %3CString%3E
+%3E
  anagram
@@ -323,439 +323,89 @@
     
-class Anagram %7B%0A            String sorted;%0A            String original;%0A%0A            Anagram(String sorted, String original) %7B%0A                this.sorted = sorted;%0A                this.original = original;%0A            %7D%0A        %7D%0A%0A        List%3CAnagram%3E sortedStrings = new ArrayList%3C%3E();%0A        for (String s : strs) %7B%0A            if (s.isEmpty()) %7B%0A                sortedStrings.add(new Anagram(s, s));%0A            %7D else %7B%0A    
+Map%3CString, List%3CString%3E%3E map = new HashMap%3C%3E();%0A        for (String s : strs) %7B%0A
     
@@ -456,20 +456,16 @@
         
- 
-   
 Arrays.s
@@ -492,42 +492,24 @@
     
-    sortedStrings.add(new Anagram(
+String sorted = 
 new 
@@ -525,13 +525,8 @@
 ars)
-, s))
 ;%0A  
@@ -527,33 +527,63 @@
 s);%0A            
-%7D
+if (!map.containsKey(sorted)) %7B
 %0A        %7D%0A%0A    
@@ -579,11 +579,8 @@
     
-%7D%0A%0A
     
@@ -596,21 +596,19 @@
 String%3E 
-resul
+lis
 t = new 
@@ -634,224 +634,127 @@
     
-Set%3CInteger%3E ignored = new HashSet%3C%3E();%0A        for (int i = 0; i %3C sortedStrings.size(); i++) %7B%0A            for (int j = 0; j %3C sortedStrings.size(); j++) %7B%0A                if (i == j) %7B%0A                    continue
+        list.add(s);%0A                map.put(sorted, list);%0A            %7D else %7B%0A                map.get(sorted).add(s)
 ;%0A  
@@ -755,33 +755,39 @@
 s);%0A            
+%7D%0A    
  
-
    %7D%0A           
@@ -787,323 +787,120 @@
     
-        if (sortedStrings.get(i).sorted.equals(sortedStrings.get(j).sorted)) %7B%0A                    if (!ignored.contains(i)) %7B%0A                        result.add(sortedStrings.get(i).original);%0A                    %7D%0A                    ignored.add(i);%0A                %7D%0A            %7D%0A        %7D%0A        return result
+return map.values().stream()%0A            .map(e -%3E new ArrayList%3C%3E(e))%0A            .collect(Collectors.toList())
 ;%0A  

Update problem 49