Anagrams Mar 19 '12
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
public class Solution { public ArrayListanagrams(String[] strs) { // Start typing your Java solution below // DO NOT write main() function Map > map = new HashMap >(); ArrayList rel = new ArrayList (); for( int i=0; i ll = new LinkedList (); ll.add( strs[i] ); map.put(s, ll); } } for(LinkedList ll : map.values() ) { if( ll.size() > 1) { rel.addAll( ll ); } } return rel; }}
public class Solution { public ArrayListanagrams(String[] strs) { // Start typing your Java solution below // DO NOT write main() function ArrayList rel = new ArrayList (); if( strs.length <=1) return rel; Map > ana = new HashMap >(); int sz = strs.length; for(int i=0; i temp = new ArrayList (); temp.add( strs[i] ); ana.put(key, temp); } } for(String str : ana.keySet() ) { if( ana.get(str).size() >1){ rel.addAll( ana.get(str) ); } } return rel; }}