Skip to content

Commit caac245

Browse files
committed
[WEEK 05] group anagrams
1 parent 6885833 commit caac245

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
// ์•„๋‚˜๊ทธ๋žจ์„ ๊ทธ๋ฃน๋ณ„๋กœ ๋งŒ๋“ค์–ด๋ผ
4+
// ์•„๋‚˜๊ทธ๋žจ -> ๊ฐ string ์„ ๋ฐฐ์น˜ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€ ?
5+
// ๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๋ช‡๊ฐœ๊ฐ€ ์žˆ๋Š” ๊ฐ€ ?
6+
List<List<String>> answer = new ArrayList<>();
7+
Map<String, List<String>> map = new HashMap<>();
8+
9+
for ( int i = 0; i < strs.length; i ++) {
10+
char[] chars = strs[i].toCharArray();
11+
Arrays.sort(chars);
12+
13+
String s = new String(chars);
14+
map.computeIfAbsent(s, k -> new ArrayList<>()).add(strs[i]);
15+
}
16+
17+
for (List<String> group : map.values()) {
18+
answer.add(group);
19+
}
20+
21+
return answer;
22+
}
23+
}

0 commit comments

Comments
ย (0)