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 List<String> anagrams(String[] strs) {
- List<String> res = new ArrayList<String>();
- HashMap<String,Integer> map = new HashMap<String,Integer>();
- for(int i=0;i<strs.length;i++){
- char[] c = strs[i].toCharArray();
- Arrays.sort(c);
- String t = new String(c);
- if(map.containsKey(t)){
- if(!res.contains(strs[map.get(t)])){
- res.add(strs[map.get(t)]);
- }
- res.add(strs[i]);
- }else{
- map.put(t, i);
- }
- }
- return res;
- }
- }
原文链接