Java Solution: beats 98%


  • 0
    X
    public class Solution {
     	public List<List<String>> groupAnagrams(String[] strs) {
    		
    		ArrayList<List<String>> ans = new ArrayList<List<String>>();
    		HashMap<String,Integer> record = new HashMap<String,Integer>();
    		int index = 0;
    				
    		for (String a : strs) {
    			
    			char[] t1 = a.toCharArray();
    			Arrays.sort(t1);
    			String t11 = String.valueOf(t1);
    			
    			if(!record.containsKey(t11)){
    				List<String> temp = new ArrayList<String>();
    				temp.add(a);
    				ans.add(temp);
    				record.put(t11,index);
    				index++;
    			}
    			else{
    				ans.get(record.get(t11)).add(a);
    			}
    			
    		}
    
    		return ans;
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.