File tree Expand file tree Collapse file tree 3 files changed +238
-0
lines changed
solution/0200-0299/0269.Alien Dictionary Expand file tree Collapse file tree 3 files changed +238
-0
lines changed Original file line number Diff line number Diff line change @@ -288,6 +288,87 @@ public:
288288};
289289```
290290
291+ #### Go
292+
293+ ```go
294+ func alienOrder(words []string) string {
295+ g := [26][26]bool{}
296+ s := [26]bool{}
297+ cnt := 0
298+ n := len(words)
299+ for i := 0; i < n-1; i++ {
300+ for _, c := range words[i] {
301+ if cnt == 26 {
302+ break
303+ }
304+ c -= 'a'
305+ if !s[c] {
306+ cnt++
307+ s[c] = true
308+ }
309+ }
310+ m := len(words[i])
311+ for j := 0; j < m; j++ {
312+ if j >= len(words[i+1]) {
313+ return ""
314+ }
315+ c1, c2 := words[i][j]-'a', words[i+1][j]-'a'
316+ if c1 == c2 {
317+ continue
318+ }
319+ if g[c2][c1] {
320+ return ""
321+ }
322+ g[c1][c2] = true
323+ break
324+ }
325+ }
326+ for _, c := range words[n-1] {
327+ if cnt == 26 {
328+ break
329+ }
330+ c -= 'a'
331+ if !s[c] {
332+ cnt++
333+ s[c] = true
334+ }
335+ }
336+
337+ inDegree := [26]int{}
338+ for _, out := range g {
339+ for i, v := range out {
340+ if v {
341+ inDegree[i]++
342+ }
343+ }
344+ }
345+ q := []int{}
346+ for i, in := range inDegree {
347+ if in == 0 && s[i] {
348+ q = append(q, i)
349+ }
350+ }
351+ ans := ""
352+ for len(q) > 0 {
353+ t := q[0]
354+ q = q[1:]
355+ ans += string(t + 'a')
356+ for i, v := range g[t] {
357+ if v {
358+ inDegree[i]--
359+ if inDegree[i] == 0 && s[i] {
360+ q = append(q, i)
361+ }
362+ }
363+ }
364+ }
365+ if len(ans) < cnt {
366+ return ""
367+ }
368+ return ans
369+ }
370+ ```
371+
291372<!-- tabs: end -->
292373
293374<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -269,6 +269,87 @@ public:
269269};
270270```
271271
272+ #### Go
273+
274+ ```go
275+ func alienOrder(words []string) string {
276+ g := [26][26]bool{}
277+ s := [26]bool{}
278+ cnt := 0
279+ n := len(words)
280+ for i := 0; i < n-1; i++ {
281+ for _, c := range words[i] {
282+ if cnt == 26 {
283+ break
284+ }
285+ c -= 'a'
286+ if !s[c] {
287+ cnt++
288+ s[c] = true
289+ }
290+ }
291+ m := len(words[i])
292+ for j := 0; j < m; j++ {
293+ if j >= len(words[i+1]) {
294+ return ""
295+ }
296+ c1, c2 := words[i][j]-'a', words[i+1][j]-'a'
297+ if c1 == c2 {
298+ continue
299+ }
300+ if g[c2][c1] {
301+ return ""
302+ }
303+ g[c1][c2] = true
304+ break
305+ }
306+ }
307+ for _, c := range words[n-1] {
308+ if cnt == 26 {
309+ break
310+ }
311+ c -= 'a'
312+ if !s[c] {
313+ cnt++
314+ s[c] = true
315+ }
316+ }
317+
318+ inDegree := [26]int{}
319+ for _, out := range g {
320+ for i, v := range out {
321+ if v {
322+ inDegree[i]++
323+ }
324+ }
325+ }
326+ q := []int{}
327+ for i, in := range inDegree {
328+ if in == 0 && s[i] {
329+ q = append(q, i)
330+ }
331+ }
332+ ans := ""
333+ for len(q) > 0 {
334+ t := q[0]
335+ q = q[1:]
336+ ans += string(t + 'a')
337+ for i, v := range g[t] {
338+ if v {
339+ inDegree[i]--
340+ if inDegree[i] == 0 && s[i] {
341+ q = append(q, i)
342+ }
343+ }
344+ }
345+ }
346+ if len(ans) < cnt {
347+ return ""
348+ }
349+ return ans
350+ }
351+ ```
352+
272353<!-- tabs: end -->
273354
274355<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ func alienOrder (words []string ) string {
2+ g := [26 ][26 ]bool {}
3+ s := [26 ]bool {}
4+ cnt := 0
5+ n := len (words )
6+ for i := 0 ; i < n - 1 ; i ++ {
7+ for _ , c := range words [i ] {
8+ if cnt == 26 {
9+ break
10+ }
11+ c -= 'a'
12+ if ! s [c ] {
13+ cnt ++
14+ s [c ] = true
15+ }
16+ }
17+ m := len (words [i ])
18+ for j := 0 ; j < m ; j ++ {
19+ if j >= len (words [i + 1 ]) {
20+ return ""
21+ }
22+ c1 , c2 := words [i ][j ]- 'a' , words [i + 1 ][j ]- 'a'
23+ if c1 == c2 {
24+ continue
25+ }
26+ if g [c2 ][c1 ] {
27+ return ""
28+ }
29+ g [c1 ][c2 ] = true
30+ break
31+ }
32+ }
33+ for _ , c := range words [n - 1 ] {
34+ if cnt == 26 {
35+ break
36+ }
37+ c -= 'a'
38+ if ! s [c ] {
39+ cnt ++
40+ s [c ] = true
41+ }
42+ }
43+
44+ inDegree := [26 ]int {}
45+ for _ , out := range g {
46+ for i , v := range out {
47+ if v {
48+ inDegree [i ]++
49+ }
50+ }
51+ }
52+ q := []int {}
53+ for i , in := range inDegree {
54+ if in == 0 && s [i ] {
55+ q = append (q , i )
56+ }
57+ }
58+ ans := ""
59+ for len (q ) > 0 {
60+ t := q [0 ]
61+ q = q [1 :]
62+ ans += string (t + 'a' )
63+ for i , v := range g [t ] {
64+ if v {
65+ inDegree [i ]--
66+ if inDegree [i ] == 0 && s [i ] {
67+ q = append (q , i )
68+ }
69+ }
70+ }
71+ }
72+ if len (ans ) < cnt {
73+ return ""
74+ }
75+ return ans
76+ }
You can’t perform that action at this time.
0 commit comments