|
| 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