일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Substring with Concatenation of All Words
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Python
- Python Implementation
- Class
- Convert Sorted List to Binary Search Tree
- attribute
- shiba
- concurrency
- Protocol
- 파이썬
- Python Code
- 컴퓨터의 구조
- 715. Range Module
- iterator
- Generator
- 밴픽
- 30. Substring with Concatenation of All Words
- 프로그래머스
- t1
- data science
- 315. Count of Smaller Numbers After Self
- LeetCode
- 시바견
- Regular Expression
- Decorator
- kaggle
- DWG
Archives
- Today
- Total
Scribbling
[Java 101] 49. Group Anagrams with HashMap, StringBuilder 본문
Computer Science/Java
[Java 101] 49. Group Anagrams with HashMap, StringBuilder
focalpoint 2023. 2. 14. 03:55class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List> map = new HashMap<>();
for (String str: strs) {
int[] count = new int[26];
Arrays.fill(count, 0);
for (char c: str.toCharArray()) {
count[c - 'a']++;
}
StringBuilder sb = new StringBuilder();
for (int i=0; i<26; i++) {
sb.append("#");
sb.append(count[i]);
}
String key = sb.toString();
if (!map.containsKey(key))
map.put(key, new ArrayList<String>());
map.get(key).add(str);
}
return new ArrayList(map.values());
}
}
'Computer Science > Java' 카테고리의 다른 글
[Java 101] 125. Valid Palindrome with StringBuilder (0) | 2023.02.21 |
---|---|
[Java 101] 347. Top K Frequent Elements with HashMap as a counter, PriorityQueue, (0) | 2023.02.14 |
[Java 101] 1. Two Sum with HashMap as a counter (0) | 2023.02.14 |
[Java 101] 242. Valid Anagram with HashMap (0) | 2023.02.14 |
[Java 101] 217. Contains Duplicate with HashSet, Arrays.stream (0) | 2023.02.14 |