首页 > 代码库 > CC150 9.2
CC150 9.2
9.2 Write a method to sort an array of strings so that all the anagrams are next to each other.
Use a map, the key is sorted string, value is list of anagrams using chars in the key.
List<String> sortByAnagrams(List<String> strings) { Map<String, List<String>> map; for (String s : strings) { append(map, s); } List<String> toReturn = new ArrayList<>(); for (Map.Entry entry : map) { toReturn.addAll(entry.getValue()); } return toReturn; } void append(Map<String, List<String>>map, String s) { String sortedS = sort(s); List<String> list = map.get(sortedS); if (list == null) { list = new ArrayList<>(); } list.add(s); map.put(sortedS, list); } String sort(String s) { // Any sorting method }
CC150 9.2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。