1. ๋ฌธ์ : https://leetcode.com/problems/group-anagrams/description/
2. ํ์ด
key๋ฅผ ๋ง๋ค์ด ํ์ฉํ๋๊ฒ ์ค์
key๊น์ง ๋น๊ตํ ํ์ x
3. ์ฝ๋
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, Integer> map = new HashMap<>();
List<List<String>> items = new ArrayList();
int k = 0;
for(String str : strs){
// key ์์ฑํ๊ธฐ.
char[] strChars = str.toCharArray();
Arrays.sort(strChars);
String key = new String(strChars);
if(map.get(key) != null){
int n = map.get(key);
List<String> item = items.get(n);
item.add(str);
items.set(n, item);
}else{
map.put(key, k++);
ArrayList<String> item = new ArrayList<>();
item.add(str);
items.add(item);
}
}
return items;
}
}
'๐ป Coding Problems Solving > Hash | HashMap' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] First Unique Character in a String (0) | 2023.07.20 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฐฉ๋ฌธ๊ธธ์ด (์๋ฐ java) (0) | 2023.03.12 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (java) (0) | 2023.03.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 [3์ฐจ] ์์ถ (0) | 2022.04.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2022.04.12 |
์ต๊ทผ๋๊ธ