forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.go
More file actions
105 lines (101 loc) ยท 4.67 KB
/
Copy pathobzva.go
File metadata and controls
105 lines (101 loc) ยท 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
ํ์ด
- ๋ ๋จ์ด๋ฅผ ์ํ๋ฒณ ํ๋์ฉ ์ฐจ๋ก๋๋ก ๋น๊ตํ์ ๋, ์ฒซ๋ฒ์งธ ์ฐจ์ด๊ฐ ๋ฐ์ํ๋ ์ง์ ์์ alien dictionary์ order๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค
- ์ฒซ๋ฒ์งธ ๋จ์ด๋ถํฐ ๋ฐ๋ก ๊ทธ ๋ค์ ๋จ์ด์ ๋ ๋จ์ด์ฉ ์ง์ง์ด์ ๋น๊ตํ๋ฉด ์์์ ๋งํ ์ผ๋ จ์ order๋ฅผ ์ฐพ์๋ผ ์ ์์ต๋๋ค
์ํ๋ฒณ x๊ฐ ์ํ๋ฒณ y๋ณด๋ค alien dictionary order์์ ์์๋ ๊ด๊ณ, ์ฆ x->y์ธ ๊ด๊ณ๋ฅผ ์ฐพ์์ x: {y1, y2, y3, ...}์ธ ์งํฉ์ map์ ๋ง๋ค๊ฒ ์ต๋๋ค
๊ทธ๋ฆฌ๊ณ ์ด๋ฅผ nextLetters๋ผ๊ณ ๋ช
๋ช
ํ์์ต๋๋ค
- ๋ง์ฝ ํน์ ์ํ๋ฒณ x์ ๋ํด, z->x์ธ ์ํ๋ฒณ z๊ฐ ์๋ค๋ฉด x๋ ์ฐ๋ฆฌ๊ฐ ์ ๋ต์ผ๋ก ์ ์ถํ result string์ ์ด๋ ์์น์๋ ์์ ๋กญ๊ฒ ๋ผ์๋ฃ์ ์ ์์ต๋๋ค
(* If there are multiple solutions, return any of them.)
์ฐ๋ฆฌ๋ ์ด๋ฐ ์ํ๋ฒณ x๋ฅผ ์ฐพ์๋ผ ๋๋ง๋ค ๋ฐ๋ก๋ฐ๋ก result string res์ ์ถ๊ฐํ๊ฒ ์ต๋๋ค
- z->x์ธ ์ํ๋ฒณ z๊ฐ ํ์ฌ ์๋์ง ์๋์ง์ ๋ํ ์ํ๊ด๋ฆฌ๋ฅผ ํ๊ธฐ ์ํด์ prevLetterCounts๋ผ๋ map์ ๋ง๋ค๊ฒ ์ต๋๋ค
prevLetterCounts[x]: z->x์ธ z์ ๊ฐ์
- nextLetters, prevLetterCounts๋ฅผ ์ ์์ฑํ ํ์๋ prevLetterCount๊ฐ 0์ธ ์ํ๋ฒณ๋ถํฐ queue์ ๋ฑ๋ก์ํจ ํ BFS๋ฅผ ์คํํฉ๋๋ค
BFS๋ฅผ ์คํํ๋ฉฐ prevLetterCount๊ฐ 0์ธ ์ํ๋ฒณ์ด ์๋ก ๋ฐ๊ฒฌ๋ ๊ฒฝ์ฐ queue์ ๋ฑ๋ก์ํต๋๋ค
- ์ฃ์ง์ผ์ด์ค๊ฐ ๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋๋ฐ,
์ฒซ๋ฒ์งธ๋ nextLetters๋ฅผ ์์ฑํ๋ ๋ฐ๋ณต๋ฌธ์์ ๋ฐ๊ฒฌ๋ฉ๋๋ค
๋๋ฒ์งธ ๋จ์ด๊ฐ ์ฒซ๋ฒ์งธ ๋จ์ด์ prefix์ธ ๊ฒฝ์ฐ๋ ์ ์ด๋ถํฐ ์๋ชป๋ dictionary order์ธ ๊ฒฝ์ฐ์
๋๋ค
์ ๊ฒฝ์ฐ๋ ๋จ์ ์ํ๋ฒณ ๋น๊ต๋ก๋ ๋ฐ๊ฒฌํ๊ธฐ ์ด๋ ค์ฐ๋ฏ๋ก flag๋ฅผ ์ฌ์ฉํ์์ต๋๋ค
๋๋ฒ์งธ๋ result string์ ๊ธธ์ด๊ฐ input์ผ๋ก ์ฃผ์ด์ง ๋จ์ด๋ค์ ๋ฑ์ฅํ ์ํ๋ฒณ์ ๊ฐ์๋ณด๋ค ์ ์ ๊ฒฝ์ฐ์
๋๋ค
์ด ๊ฒฝ์ฐ๋ nextLetters์ ์ํ์ด ๋ฐ์ํ ๊ฒฝ์ฐ์ด๋ฏ๋ก dictionary order๊ฐ ์๋ชป๋์๋ค๊ณ ํ๋จํ ์ ์์ต๋๋ค
Big O
- N: ์ฃผ์ด์ง ๋ฐฐ์ด words์ ๊ธธ์ด
- S(W): ๋ฐฐ์ด words์ ์ํ ๋ชจ๋ string์ ๊ธธ์ด์ ์ดํฉ
- Time complexity: O(N + S(W))
- prevLetterCounts์ nextLetters ์์ฑ -> O(N)
- nextLetters์ ๋ค์ด๊ฐ ์ํ๋ฒณ ์ ํ๊ด๊ณ ์ฐพ๊ธฐ -> O(S(W))
- ์ํ๋ฒณ ์๋ฌธ์์ ์๋ ์ ํ๋์ด ์๊ธฐ ๋๋ฌธ์ BFS์ ์๊ฐ ๋ณต์ก๋ ์ํ์ ์ ์ ํด์ ธ ์์ต๋๋ค -> O(26 * 26) = O(1)
- Space complexity: O(1)
- ์ํ๋ฒณ ์๋ฌธ์์ ์๋ ์ ํ๋์ด ์๊ธฐ ๋๋ฌธ์ ๊ณต๊ฐ ๋ณต์ก๋์ ์ํ์ ์ ์ ํด์ ธ ์์ต๋๋ค
prevLetterCounts -> O(26) = O(1)
nextLetters -> O(26 * 26) = O(1)
queue -> O(26) = O(1)
*/
import "strings"
func alienOrder(words []string) string {
n := len(words)
// prevLetterCounts[x] = count of letters y that are in relation of y->x
prevLetterCounts := make(map[string]int)
// nextLetters[x] = set of letters y that are in relation of x->y
nextLetters := make(map[string]map[string]bool)
for _, word := range words {
for _, c := range word {
if _, ok := prevLetterCounts[string(c)]; !ok {
prevLetterCounts[string(c)] = 0
nextLetters[string(c)] = make(map[string]bool)
}
}
}
for i := 0; i < n-1; i++ {
currWord := words[i]
nextWord := words[i+1]
// flag for edge case below
diff := false
for j := 0; j < len(currWord) && j < len(nextWord); j++ {
if currWord[j] != nextWord[j] {
diff = true
if _, ok := nextLetters[string(currWord[j])][string(nextWord[j])]; !ok {
prevLetterCounts[string(nextWord[j])]++
nextLetters[string(currWord[j])][string(nextWord[j])] = true
}
break
}
}
// tricky edge case!!!
// if nextWord is prefix of currWord, then the provided dictionary order is invalid
if !diff && len(currWord) > len(nextWord) {
return ""
}
}
// BFS
queue := make([]string, 0, len(prevLetterCounts))
for letter := range prevLetterCounts {
// we can arrange letters whose prevLetterCount is zero as we wish
if prevLetterCounts[letter] == 0 {
queue = append(queue, letter)
}
}
// in Go, using strings.Builder is the most efficient way to build strings
var sb strings.Builder
for len(queue) > 0 {
// pop the letter from the queue and append it to the result string
popped := queue[0]
queue = queue[1:]
sb.WriteString(popped)
for nextLetter := range nextLetters[popped] {
prevLetterCounts[nextLetter]--
// if prevLetterCount for nextLetter becomes zero, we can add it to the queue
// append to the result string (order) in the next iteration
if prevLetterCounts[nextLetter] == 0 {
queue = append(queue, nextLetter)
}
}
}
// res is result string
res := sb.String()
// this case means that there was a cycle
if len(res) != len(prevLetterCounts) {
return ""
}
// else return res
return res
}