标签:leetcode
https://oj.leetcode.com/problems/anagrams/
http://blog.csdn.net/linhuanmars/article/details/21664747
public class Solution { public List<String> anagrams(String[] strs) { int len = strs.length; Map<Map<Integer, Integer>, List<String>> map = new HashMap<>(); for (String s : strs) { Map<Integer, Integer> pattern = pattern(s); List<String> list = map.get(pattern); if (list == null) list = new ArrayList<String>(); list.add(s); map.put(pattern, list); } List<String> toReturn = new ArrayList<>(); for (List<String> lists : map.values()) { if (lists.size() > 1) { toReturn.addAll(lists); } } return toReturn; } private static final int CHAR_SIZE = 26; // Assume ‘a - z‘ private Map<Integer, Integer> pattern(String s) { Map<Integer, Integer> pattern = new HashMap<>(); for (char c : s.toCharArray()) { int v = c - ‘a‘; Integer occr = pattern.get(v); if (occr == null) occr = 0; occr ++; pattern.put(v, occr); } return pattern; } }
标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1598615