首页 > 代码库 > Anagrams
Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
<pre name="code" class="java">public class Solution { public List<String> anagrams(String[] strs) { Map<String, Integer> map = new HashMap<>(); List<String> res = new ArrayList<String>(); if (strs.length == 0) { return res; } for (int i = 0; i < strs.length; i++) { char[] c = strs[i].toCharArray(); Arrays.sort(c); String s = Arrays.toString(c); if (!map.containsKey(s)) { map.put(s, 1); } else { map.put(s, map.get(s) + 1); } } for (int i = 0; i < strs.length; i++) { char[] c = strs[i].toCharArray(); Arrays.sort(c); String s = Arrays.toString(c); if (map.containsKey(s) && map.get(s) >= 2) { res.add(strs[i]); } } return res; } }
Anagrams
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。