-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlien Dictionary.py
98 lines (83 loc) · 2.74 KB
/
Alien Dictionary.py
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
'''
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
Example 1:
Input:
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
Output: "wertf"
Example 2:
Input:
[
"z",
"x"
]
Output: "zx"
Example 3:
Input:
[
"z",
"x",
"z"
]
Output: ""
Explanation: The order is invalid, so return "".
Note:
You may assume all letters are in lowercase.
You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.
'''
class Solution(object):
def alienOrder(self, words):
"""
:type words: List[str]
:rtype: str
"""
parent_count = {}
children = {}
char_set = set()
for word in words:
for c in word:
parent_count[c] = 0
char_set.add(c)
for i in xrange(len(words)):
for j in xrange(i+1, len(words)):
for k in xrange(min(len(words[i]), len(words[j]))):
if words[i][k] != words[j][k]:
if words[i][k] not in children:
children[words[i][k]] = [words[j][k]]
if words[j][k] not in parent_count:
parent_count[words[j][k]] = 1
else:
parent_count[words[j][k]] += 1
else:
if words[j][k] not in children[words[i][k]]:
children[words[i][k]].append(words[j][k])
if words[j][k] not in parent_count:
parent_count[words[j][k]] = 1
else:
parent_count[words[j][k]] += 1
break
res = []
vec = []
for k in parent_count:
if parent_count[k] == 0:
vec.append(k)
while vec:
next_vec = []
for c in vec:
res.append(c)
if c in children:
for child in children[c]:
parent_count[child] -= 1
if parent_count[child] == 0:
next_vec.append(child)
vec = next_vec
if len(res) != len(char_set):
return ''
return ''.join(res)