首页 > 代码库 > LeetCode Anagrams My solution
LeetCode Anagrams My solution
Anagrams
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> result = new ArrayList<String>(); if (strs == null || strs.length == 0 ) { return result; } HashMap<String,Integer> hmaps = new HashMap<String,Integer>(); for (int i = 0; i < strs.length; i++) { String curSort = sortString(strs[i]); if (hmaps.containsKey(curSort)) { hmaps.put(curSort, hmaps.get(curSort) + 1); } else { hmaps.put(curSort, 1); } } for(int i = 0; i < strs.length; i++) { if (hmaps.containsKey(sortString(strs[i])) && hmaps.get(sortString(strs[i])) > 1) { result.add(strs[i]); } } return result; } String sortString(String str) { char [] charArr = str.toCharArray(); Arrays.sort(charArr); return Arrays.toString(charArr); } }
LeetCode Anagrams My solution
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。