Skip to content

Commit

Permalink
feat: group-anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
Invidam committed May 25, 2024
1 parent 5dac1da commit 7265665
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions group-anagrams/invidam.go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Intuition
์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ทธ๋ฃน ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•œ๋‹ค.
# Approach
1. `{์ •๋ ฌ๋œ ๋ฌธ์ž์—ด, ๊ทธ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค}`๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
2. ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ, ์ธ๋ฑ์Šค๋ฅผ struct๋กœ ํ•˜์—ฌ ๋ฐฐ์—ด์— ์‚ฝ์ž…ํ•œ๋‹ค.
- ์ธ๋ฑ์Šค๋ฅผ ์œ ์ง€ํ•˜๋Š” ์ด์œ ๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค. (์ •๋ ฌ์„ ํ–ˆ๊ธฐ์— ์ด๋ ‡์ง€ ์•Š์œผ๋ฉด ํ™•์ธ์ด ์–ด๋ ต๋‹ค.)
3. ๋ชจ๋“  struct๊ฐ€ ์‚ฝ์ž…๋œ ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
4. ๋ฐ˜๋ณต๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉฐ `์ด์ „ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€์ง€` ์—ฌ๋ถ€๋ฅผ ๊ทธ๋ฃน์„ ํŒ๋‹จํ•˜์—ฌ ์ธ๋ฑ์Šค๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•ด ๋ชจ์•„๋†“๋Š”๋‹ค.
5. ๊ทธ๋ฃจํ•‘ํ•œ ์ธ๋ฑ์Šค๋“ค์„ ๋ฌธ์ž์—ด(์›๋ณธ ๋ฌธ์ž์—ด์„ ์ฐธ์กฐํ•ด์„œ)๋กœ ์น˜ํ™˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(nklog(n))$$ -->
: ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`. ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด๋‹ค.
- Space complexity: $$O(nk)$$

: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ธธ์ด `n`, ๋ฐฐ์—ด์ด ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด `k`, (์ฝ”๋“œ๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด, ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ ๋ชจ๋‘ `n`, `k`์ด๋‹ค.)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```go
type StrAndIdx struct {
Str string
Idx int
}

func sortSring(s string) string {
runes := []rune(s)

sort.Slice(runes, func(i, j int) bool {
return runes[i] < runes[j]
})

return string(runes)
}

func groupAnagrams(strs []string) [][]string {

strAndIdxs := make([]StrAndIdx, 0, len(strs)+5)

for i, s := range strs {
strAndIdxs = append(strAndIdxs, StrAndIdx{sortSring(s), i})
}

sort.Slice(strAndIdxs, func(i, j int) bool {
return strAndIdxs[i].Str < strAndIdxs[j].Str
})

groupedIdxs := make([][]int, 0, len(strAndIdxs)/4)

group := make([]int, 0)
defaultString := "NOT_EXIST_STRING"
prev := defaultString
for _, sAI := range strAndIdxs {
curr := sAI.Str
idx := sAI.Idx
if prev == curr {
group = append(group, idx)
} else {
if prev != defaultString {
groupedIdxs = append(groupedIdxs, group)
}
group = []int{idx}
}
prev = curr
}

if len(group) != 0 {
groupedIdxs = append(groupedIdxs, group)
}

groupedStrs := make([][]string, 0, len(groupedIdxs))
for _, idxs := range groupedIdxs {

groupStr := make([]string, 0, len(idxs))
for _, idx := range idxs {
groupStr = append(groupStr, strs[idx])
}
groupedStrs = append(groupedStrs, groupStr)
}

return groupedStrs
}
```

# **To** Learn
- GoLang์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋น„๊ตํ•˜๋Š”์ง€.
- ํ•ด๋‹น ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์žˆ์–ด์„œ ์‚ฝ์งˆํ•œ ๊ฒƒ์€ ๋ฌด์—‡์ด์—ˆ๋Š”์ง€. ์•ž์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ์•ˆํ•  ์ˆ˜ ์žˆ๋Š”์ง€.

0 comments on commit 7265665

Please sign in to comment.