import java.util.*; /** * create a map <key,value> * key is #0#4#1#7... 26 total * value is these strings * hint: calculate times of per character and then compare them */ class Solution{ public List<List<String>> groupAnagrams(String[] strs){ if(strs.length == 0){ return new ArrayList(); } Map<String, List> res = new HashMap<String, List>(); int[] count = new int[26]; for(String s : strs){ Arrays.fill(count,0); for(char c : s.toCharArray()){ count[c - 'a']++; } StringBuilder sb = new StringBuilder(""); for(int i = 0; i < 26; i++){ sb.append('#'); sb.append(count[i]); } String key = sb.toString(); if(!res.containsKey(key)){ res.put(key,new ArrayList()); } res.get(key).add(s); } return new ArrayList(res.values()); } }
49
猜你喜欢
转载自www.cnblogs.com/zhaijiayu/p/11541578.html
今日推荐
周排行