공부기록

49. Group Anagrams 본문

코테/LeetCode

49. Group Anagrams

코타쿠 2022. 4. 19. 10:32

문제

https://leetcode.com/problems/group-anagrams/

코드

import java.util.*;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> m = new HashMap<>();
        List<List<String>> res = new ArrayList<>();
        for(String str : strs){
            char[] tmp = str.toCharArray();
            Arrays.sort(tmp);
            String k = new String(tmp);
            if(m.get(k) == null)
                m.put(k, new ArrayList<>());
            m.get(k).add(str);
        }

        for(String key : m.keySet())
            res.add(m.get(key));

        return res;

    }
}

피드백

'코테 > LeetCode' 카테고리의 다른 글

821. Shortest Distance to a Character  (0) 2022.07.08
74. Search a 2D Matrix  (0) 2022.04.21
347. Top K Frequent Elements  (0) 2022.04.16
179. Largest Number  (0) 2022.04.15
384. Shuffle an Array  (0) 2022.04.09