-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem269.py
More file actions
64 lines (56 loc) · 2.17 KB
/
problem269.py
File metadata and controls
64 lines (56 loc) · 2.17 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
class Solution(object):
def alienOrder(self, words):
import collections
sentences = [words]
relationship = collections.defaultdict(list)
distinctCharacters = set([])
for word in words:
distinctCharacters = distinctCharacters.union(set(word))
while sentences:
stack = []
for sentence in sentences:
if len(sentence) == 1:
string = sentence[0]
for charac in string:
relationship[charac] = relationship[charac]
samePrefixWord = collections.defaultdict(list)
prefix = []
for word in sentence:
prefix.append(word[0])
if word[1:] != '':
samePrefixWord[word[0]].append(word[1:])
for i in range(len(prefix)-1):
for k in range(i+1,len(prefix)):
if prefix[i] == prefix[k]:
continue
if prefix[i] in relationship[prefix[k]]:
return ''
relationship[prefix[i]].append(prefix[k])
for value in samePrefixWord.values():
stack.append(value)
sentences = stack
inDegree = collections.defaultdict(int)
for key in relationship:
value = relationship[key]
for charac in value:
inDegree[charac] += 1
inDegree[key] += 0
for charac in distinctCharacters:
if charac not in inDegree:
inDegree[charac] = 0
zeroDegree = []
for key in inDegree:
if inDegree[key] == 0:
zeroDegree.append(key)
rst = ''
while zeroDegree:
currentNode = zeroDegree.pop(0)
rst += currentNode
for charac in relationship[currentNode]:
inDegree[charac] -= 1
if inDegree[charac] == 0:
zeroDegree.append(charac)
return rst if len(rst) == len(distinctCharacters) else ''
s = Solution()
words = ['z','z']
print s.alienOrder(words)