Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //高频班version
- public class Solution {
- /**
- * @param strs: the given array of strings
- * @return: The anagrams which have been divided into groups
- */
- public List<List<String>> groupAnagrams(String[] strs) {
- // write your code here
- Map<String, List<String>> map = new HashMap<>();
- for (String s : strs) {
- char[] sc = s.toCharArray();
- Arrays.sort(sc);
- String key = String.valueOf(sc);
- map.putIfAbsent(key, new ArrayList<>());
- map.get(key).add(s);
- }
- return new ArrayList<>(map.values());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment