首页 > 代码库 > [LeetCode]49 Anagrams
[LeetCode]49 Anagrams
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]49 Anagrams
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。