Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions solution/0200-0299/0269.Alien Dictionary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,87 @@ public:
};
```

#### Go

```go
func alienOrder(words []string) string {
g := [26][26]bool{}
s := [26]bool{}
cnt := 0
n := len(words)
for i := 0; i < n-1; i++ {
for _, c := range words[i] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}
m := len(words[i])
for j := 0; j < m; j++ {
if j >= len(words[i+1]) {
return ""
}
c1, c2 := words[i][j]-'a', words[i+1][j]-'a'
if c1 == c2 {
continue
}
if g[c2][c1] {
return ""
}
g[c1][c2] = true
break
}
}
for _, c := range words[n-1] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}

inDegree := [26]int{}
for _, out := range g {
for i, v := range out {
if v {
inDegree[i]++
}
}
}
q := []int{}
for i, in := range inDegree {
if in == 0 && s[i] {
q = append(q, i)
}
}
ans := ""
for len(q) > 0 {
t := q[0]
q = q[1:]
ans += string(t + 'a')
for i, v := range g[t] {
if v {
inDegree[i]--
if inDegree[i] == 0 && s[i] {
q = append(q, i)
}
}
}
}
if len(ans) < cnt {
return ""
}
return ans
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
81 changes: 81 additions & 0 deletions solution/0200-0299/0269.Alien Dictionary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,87 @@ public:
};
```

#### Go

```go
func alienOrder(words []string) string {
g := [26][26]bool{}
s := [26]bool{}
cnt := 0
n := len(words)
for i := 0; i < n-1; i++ {
for _, c := range words[i] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}
m := len(words[i])
for j := 0; j < m; j++ {
if j >= len(words[i+1]) {
return ""
}
c1, c2 := words[i][j]-'a', words[i+1][j]-'a'
if c1 == c2 {
continue
}
if g[c2][c1] {
return ""
}
g[c1][c2] = true
break
}
}
for _, c := range words[n-1] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}

inDegree := [26]int{}
for _, out := range g {
for i, v := range out {
if v {
inDegree[i]++
}
}
}
q := []int{}
for i, in := range inDegree {
if in == 0 && s[i] {
q = append(q, i)
}
}
ans := ""
for len(q) > 0 {
t := q[0]
q = q[1:]
ans += string(t + 'a')
for i, v := range g[t] {
if v {
inDegree[i]--
if inDegree[i] == 0 && s[i] {
q = append(q, i)
}
}
}
}
if len(ans) < cnt {
return ""
}
return ans
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
76 changes: 76 additions & 0 deletions solution/0200-0299/0269.Alien Dictionary/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
func alienOrder(words []string) string {
g := [26][26]bool{}
s := [26]bool{}
cnt := 0
n := len(words)
for i := 0; i < n-1; i++ {
for _, c := range words[i] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}
m := len(words[i])
for j := 0; j < m; j++ {
if j >= len(words[i+1]) {
return ""
}
c1, c2 := words[i][j]-'a', words[i+1][j]-'a'
if c1 == c2 {
continue
}
if g[c2][c1] {
return ""
}
g[c1][c2] = true
break
}
}
for _, c := range words[n-1] {
if cnt == 26 {
break
}
c -= 'a'
if !s[c] {
cnt++
s[c] = true
}
}

inDegree := [26]int{}
for _, out := range g {
for i, v := range out {
if v {
inDegree[i]++
}
}
}
q := []int{}
for i, in := range inDegree {
if in == 0 && s[i] {
q = append(q, i)
}
}
ans := ""
for len(q) > 0 {
t := q[0]
q = q[1:]
ans += string(t + 'a')
for i, v := range g[t] {
if v {
inDegree[i]--
if inDegree[i] == 0 && s[i] {
q = append(q, i)
}
}
}
}
if len(ans) < cnt {
return ""
}
return ans
}