๋ฌธ์ ๋งํฌ
https://leetcode.com/problems/group-anagrams/
Group Anagrams - LeetCode
Can you solve this real interview question? Group Anagrams - Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase
leetcode.com
'eat','aet','tae' ๋ฑ์ผ๋ก ๋์ผํ ์บ๋ฆญํฐ๋ก ์์๋ง ๋ฐ๊ฟ์ ๋์์๋,
ํด๋น ๋ฌธ์์ด๋ค์ ๋ฆฌ์คํธ๋ก ๋ชจ์์ ๋ฟ๋ ค์ฃผ๋ ๋ฌธ์ ์ด๋ค.
๋ด๊ฐ ๋จผ์ ์๊ฐํ๋ ๊ฑฐ๋ e1a1t1 ๋ฑ์ผ๋ก ๊ฐ๊ฐ ๋ฌธ์์ ์นด์ดํธ๋ฅผ ๋์ดํ๋ค์์ ์ด๊ฑฐ๋ฅผ ํค๋ก ํด์ ํด์๋งต์ ๋ง๋ค์ด์ ๊ฐ๋ฉด ๋ ๊ฑฐ ๊ฐ๋ค๊ณ ์๊ฐํ๋๋ฐ,
๊ตฌํํ๋ ๊ณผ์ ์์ a1e1t1๊ณผ a1t1e1 ์ ๊ฐ์ key๋ก ์ค์ ํ๋ ๊ณผ์ ์์ ์ ๋ฅผ ๋จน์ด์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์๋ํ๊ธฐ๋ก ํ๋ค.
1์ฐจ ํ์ด
1. ๋จผ์ strs๋ฅผ ๋๋ฉด์ ์ํ๋ฒณ์ ์ ๋ ฌํด์ ์๋ก์ด ๋ฐฐ์ด sortedStrs์ ์ ์ฅํ๋ค. (eat -> aet, tae -> aet)
2. sortStrs๋ฅผ ๋ค์ ์ ๋ ฌํ๋ค. ๊ทธ๋ฌ๋ฉด ๋์ผํ ๊ฒ๋ผ๋ฆฌ ๋ฌถ์ ์ ์๊ฒ ๋๋ค.
3. sortedStr๋ฅผ ๋๋ฉด์, ๊ฒฐ๊ณผ๊ฐ results์ ๋ง์ง๋ง ๊ฐ๊ณผ ํ์ฌ ๊ฐ์ด ์ผ์นํ๋ฉด ๊ทธ ์ ์ ๋ฃ์ด์ฃผ๊ณ ์๋๋ฉด ์๋ก์ด ๋ฐฐ์ด์ ๋ฃ์ด์ค๋ค.
# 1. ๋จผ์ strs๋ฅผ ๋๋ฉด์ ์ํ๋ฒณ์ ์ ๋ ฌํด์ ์๋ก์ด ๋ฐฐ์ด sortedStrs์ ์ ์ฅํ๋ค. (eat -> aet, tae -> aet)
sortedStrs = []
for str in strs:
# ๋๋ฉด์ eat -> aet ์ํ๋ฒณ์์ผ๋ก ์ ๋ ฌ
sortedStrs.append((str, ''.join(sorted(str))))
# 2. sortStrs๋ฅผ ๋ค์ ์ ๋ ฌํ๋ค. ๊ทธ๋ฌ๋ฉด ๋์ผํ ๊ฒ๋ผ๋ฆฌ ๋ฌถ์ ์ ์๊ฒ ๋๋ค.
sortedStrs.sort(key = lambda x:x[1])
# 3. sortedStr๋ฅผ ๋๋ฉด์, ๊ฒฐ๊ณผ๊ฐ results์ ๋ง์ง๋ง ๊ฐ๊ณผ ํ์ฌ ๊ฐ์ด ์ผ์นํ๋ฉด ๊ทธ ์ ์ ๋ฃ์ด์ฃผ๊ณ ์๋๋ฉด ์๋ก์ด ๋ฐฐ์ด์ ๋ฃ์ด์ค๋ค.
results = [[sortedStrs[0][0]]]
# ๋์ผํ ๊ฒ๋ผ๋ฆฌ ๋ฌถ์ด์ฃผ๊ธฐ
for i in range(1, len(sortedStrs)):
originalValue = sortedStrs[i][0]
checkValue = sortedStrs[i][1]
# ์ ์ ๊ฐ๊ณผ ๋์ผํ๋ค๋ฉด, ์ ๋ฐฐ์ด์ ๋ฃ์ด์ฃผ๊ณ
if sortedStrs[i - 1][1] == checkValue:
results[-1].append(originalValue)
# ์ ์ ๊ฐ๊ณผ ๋ค๋ฅด๋ฉด, ์๋ก์ด ๋ฐฐ์ด์ ์ถ๊ฐ
else:
results.append([originalValue])
๋ง์ง๋ง 3๋ฒ ๊ณผ์ ์ด ๋ณต์กํ๊ธด ํ๋ฐ, ์ง๊ธ์์ ์๊ฐํด๋ณด๋ฉด defaultdict๋ฅผ ์ด์ฉํด์ ์ฝ๊ฒ ์ ๊ทผํ ์ ์์ง ์์๋ ์ถ๋ค.
2์ฐจ ํ์ด (ํด์ค ์ฐธ๊ณ )
ํด์ค ์ ํฌ๋ธ๋ ์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ค.
https://www.youtube.com/watch?v=vzdNOK2oB2E
์ผ๋จ ์ ๊ทผ ๋ฐฉ์ ์์ฒด๋ ์ฒซ๋ฒ์งธ ์๊ฐํ๋ ๋ฐฉ์๊ณผ ์ ์ฌํ๋ค.
๋ด๊ฐ ์๊ฐํ์ง ๋ชปํ๋ ๊ฒ์ 1. list๋ฅผ tuple๋ก ๋ฐ๊ฟ์ dictionary์ key๋ก ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ. 2. ์ํ๋ฒณ์ ๊ฐฏ์ count ์ [0] * 26 ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ด์๋ค.
ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
1. ๋จผ์ strs๋ฅผ ๋๋ฉด์ count ๋ฐฐ์ด์ ์ํ๋ฒณ ์๋ฅผ ๊ฐ๊ฐ ์นด์ดํธ ํ๋ค.
์ด๋, count ๋ฐฐ์ด์ 0,1,2,...26์ ์ํ๋ฒณ a,....z์ ํด๋นํ ์ ์๋๋ก ord๋ผ๋ ์์คํค๋ฐฐ์ด ๊ฐ์ ๊ฐ์ ธ์ค๋ ํจ์๋ฅผ ์ด๋ค.
2. ๊ทธ ๋ค์ count๋ฐฐ์ด์ tuple๋ก ๋ฐ๊ฟ results ๋ฐฐ์ด์ key๋ก ์ฌ์ฉํด ์ ์ฅํ๋ค.
3. results ๋ฅผ ๋ฆฌํดํ๋ค.
# ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ผ๋ก ๊ฐ๋ ๋์
๋๋ฆฌ๋ฅผ ์์ฑํ๋ฉด, ๋์
๋๋ฆฌ์ ํค๋ฅผ ์ถ๊ฐํ ๋ ํค๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ๋น ๋ฆฌ์คํธ๊ฐ ์๋์ผ๋ก ์์ฑ
results = defaultdict(list)
# 1. ๋จผ์ strs๋ฅผ ๋๋ฉด์ count ๋ฐฐ์ด์ ์ํ๋ฒณ ์๋ฅผ ๊ฐ๊ฐ ์นด์ดํธ ํ๋ค.
## ์ด๋, count ๋ฐฐ์ด์ 0,1,2,...26์ ์ํ๋ฒณ a,....z์ ํด๋นํ ์ ์๋๋ก ord๋ผ๋ ์์คํค๋ฐฐ์ด ๊ฐ์ ๊ฐ์ ธ์ค๋ ํจ์๋ฅผ ์ด๋ค.
for s in strs:
count = [0] * 26 # ์ํ๋ฒณ ๋งํผ ๋ง๋ค๊ธฐ
for c in s:
# ์์คํค ์ฝ๋์์ 'a'๋ 97์ด๊ณ , 'b'๋ 98, 'c'๋ 99, ์ด๋ฐ ์์ผ๋ก ๊ณ์ ์ฆ๊ฐ
count[ord(c) - ord('a')] += 1
2. ๊ทธ ๋ค์ count๋ฐฐ์ด์ tuple๋ก ๋ฐ๊ฟ results ๋ฐฐ์ด์ key๋ก ์ฌ์ฉํด ์ ์ฅํ๋ค.
# count ๋ฅผ tuple ํํ๋ก ๋ฐ๊ฟ์ผ 'unhashable type:list ์ ๋ฐ์ํจ '
results[tuple(count)].append(s)
# 3. results๋ฅผ ์ถ๋ ฅํ๋ค.
return results.values()
ํจ์ฌ ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ก๋ค !!
์ฐธ๊ณ ๋ก, ์ด ๋ฐฉ์์ java์์๋ ์ฌ์ฉ๊ฐ๋ฅํ์ง ๊ถ๊ธํด์ chatgpt์๊ฒ ๋ฌผ์ด๋ดค๋๋ฐ,
์๋์ ๊ฐ์ด array๋ฅผ string์ผ๋ก ๋ฐ๊ฟ์ key๋ก ์ฌ์ฉํ๋ฉด ๋๋ค๊ณ ํ๋ค.
String key = Arrays.toString(count);
chatgpt๊ฐ ์ ๊ณตํด์ค ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
public static List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
int[] count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}
// ๋ฐฐ์ด์ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ ํค๋ก ์ฌ์ฉ
String key = Arrays.toString(count);
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(s);
}
return new ArrayList<>(map.values());
}
์ฐธ๊ณ ํด๋ณด๋ฉด ์ข์ ๋งํฌ
https://ddoance.tistory.com/139
์๊ณ ๋ฆฌ์ฆ ํ์ด์์. neetcode ์ฐธ๊ณ ํด์ leetcode ์ค๋นํ๊ธฐ (leethub)
neetcode ์ปค๋ฆฌ์ด๋ฆฌ '๋ฆฌํธ์ฝ๋ 569๋ฌธ์ ํ๊ณ ๊ตฌ๊ธ์ ์ ์ฌํ ์ฌ๋ ์ด์ผ๊ธฐ' ์ ๋ณด๋ค๊ฐ, ์ด ์ฌ์ดํธ๋ฅผ ์๊ฒ๋์๋ค. https://neetcode.io/roadmap ์ด๋ฐ์์ผ๋ก ์ด๋ค ์์๋ก ํ๋ฉด ์ข์์ง ์์๋ฅผ ์ ๊ณตํด์ฃผ๊ณ , ๊ฐ ๋ฌธ์
ddoance.tistory.com
'๐ค ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode] 128. Longest Consecutive Sequence ์๊ฐ๋ณต์ก๋ O(n) ํ์ด, ํด์ค (python) (0) | 2023.10.21 |
---|---|
[leetcode] 36. Valid Sudoku ํ์ด, ํด์ค (python) (0) | 2023.10.18 |
์๊ณ ๋ฆฌ์ฆ ํ์ด์์. neetcode ์ฐธ๊ณ ํด์ leetcode ์ค๋นํ๊ธฐ (leethub) (0) | 2023.09.28 |
baekjoon. 2559 ์์ด [Silver III][python] (0) | 2023.08.20 |
baekjoon. 14890 ๊ฒฝ์ฌ๋ก [Gold III][python] (0) | 2023.08.13 |