Skip to content

Commit 7265665

Browse files
committed
feat: group-anagrams
1 parent 5dac1da commit 7265665

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

โ€Žgroup-anagrams/invidam.go.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Intuition
2+
์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ทธ๋ฃน ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•œ๋‹ค.
3+
# Approach
4+
1. `{์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, ๊ทธ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค}`๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
5+
2. ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ, ์ธ๋ฑ์Šค๋ฅผ struct๋กœ ํ•˜์—ฌ ๋ฐฐ์—ด์— ์‚ฝ์ž…ํ•œ๋‹ค.
6+
- ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ด์œ ๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. (์ •๋ ฌ์„ ํ–ˆ๊ธฐ์— ์ด๋ ‡์ง€ ์•Š์œผ๋ฉด ํ™•์ธ์ด ์–ด๋ ต๋‹ค.)
7+
3. ๋ชจ๋“  struct๊ฐ€ ์‚ฝ์ž…๋œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
8+
4. ๋ฐ˜๋ณต๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉฐ `์ด์ „ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€` ์—ฌ๋ถ€๋ฅผ ๊ทธ๋ฃน์„ ํŒ๋‹จํ•˜์—ฌ ์ธ๋ฑ์Šค๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•ด ๋ชจ์•„๋†“๋Š”๋‹ค.
9+
5. ๊ทธ๋ฃจํ•‘ํ•œ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌธ์ž์—ด(์›๋ณธ ๋ฌธ์ž์—ด์„ ์ฐธ์กฐํ•ด์„œ)๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
10+
# Complexity
11+
- Time complexity: $$O(n)$$
12+
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ -->
13+
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`. ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด๋‹ค.
14+
- Space complexity: $$O(nk)$$
15+
16+
: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฐฐ์—ด์ด ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`, (์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด, ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ ๋ชจ๋‘ `n`, `k`์ด๋‹ค.)
17+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
18+
19+
# Code
20+
```go
21+
type StrAndIdx struct {
22+
Str string
23+
Idx int
24+
}
25+
26+
func sortSring(s string) string {
27+
runes := []rune(s)
28+
29+
sort.Slice(runes, func(i, j int) bool {
30+
return runes[i] < runes[j]
31+
})
32+
33+
return string(runes)
34+
}
35+
36+
func groupAnagrams(strs []string) [][]string {
37+
38+
strAndIdxs := make([]StrAndIdx, 0, len(strs)+5)
39+
40+
for i, s := range strs {
41+
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i})
42+
}
43+
44+
sort.Slice(strAndIdxs, func(i, j int) bool {
45+
return strAndIdxs[i].Str < strAndIdxs[j].Str
46+
})
47+
48+
groupedIdxs := make([][]int, 0, len(strAndIdxs)/4)
49+
50+
group := make([]int, 0)
51+
defaultString := "NOT_EXIST_STRING"
52+
prev := defaultString
53+
for _, sAI := range strAndIdxs {
54+
curr := sAI.Str
55+
idx := sAI.Idx
56+
if prev == curr {
57+
group = append(group, idx)
58+
} else {
59+
if prev != defaultString {
60+
groupedIdxs = append(groupedIdxs, group)
61+
}
62+
group = []int{idx}
63+
}
64+
prev = curr
65+
}
66+
67+
if len(group) != 0 {
68+
groupedIdxs = append(groupedIdxs, group)
69+
}
70+
71+
groupedStrs := make([][]string, 0, len(groupedIdxs))
72+
for _, idxs := range groupedIdxs {
73+
74+
groupStr := make([]string, 0, len(idxs))
75+
for _, idx := range idxs {
76+
groupStr = append(groupStr, strs[idx])
77+
}
78+
groupedStrs = append(groupedStrs, groupStr)
79+
}
80+
81+
return groupedStrs
82+
}
83+
```
84+
85+
# **To** Learn
86+
- GoLang์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋Š”์ง€.
87+
- ํ•ด๋‹น ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์žˆ์–ด์„œ ์‚ฝ์งˆํ•œ ๊ฒƒ์€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์•ˆํ•  ์ˆ˜ ์žˆ๋Š”์ง€.

0 commit comments

Comments
ย (0)