Given an array of strings, group anagrams together.
For example, given: ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'],
Return:
[ ['ate', 'eat','tea'], ['nat','tan'], ['bat'] ]
Note: All inputs will be in lower-case.
I had an interview with this question just a few days ago. Using the sorted string as the key in a hashmap solves all anagram problems.
class Solution { public List> groupAnagrams(String[] strs) { HashMap map = new HashMap(); ArrayList> res = new ArrayList>(); int order = 0; for(int i=0;i tm = new ArrayList(); tm.add(strs[i]); res.add(tm); order++; } } return res; } }
This websiteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:
Original author:Jake Tao,source:"49. Group Anagrams"