Super easy to understand java solution


  • 0
    A
    public List<List<String>> groupAnagrams(String[] strs) {
    		Map<String, List<String>> resultMap = new HashMap<>();
    		for(String str : strs){
    			char[] strArray = str.toCharArray();
    			Arrays.sort(strArray);
    			String newString = new String(strArray);
    			if (resultMap.containsKey(newString)) {
    				resultMap.get(newString).add(str);
    			} else {
    				List<String> temp = new ArrayList<>();
    				temp.add(str);
    				resultMap.put(newString, temp);
    			}
    		}
    		List<List<String>> result = new ArrayList<List<String>>();
    		result.addAll(resultMap.values());
    		return result;
    	}
    

Log in to reply
 

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