Skip to content

Commit b9ebbc9

Browse files
authored
Update alien-dictionary.py
1 parent d608aa1 commit b9ebbc9

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Python/alien-dictionary.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def alienOrder(self, words):
1515
nodes.add(c)
1616

1717
for i in xrange(1, len(words)):
18+
if len(words[i-1]) > len(words[i]) and \
19+
words[i-1][:len(words[i])] == words[i]:
20+
return ""
1821
self.findEdges(words[i - 1], words[i], in_degree, out_degree)
1922

2023
for node in nodes:
@@ -68,6 +71,9 @@ def alienOrder(self, words):
6871
for node in nodes:
6972
ancestors[node] = []
7073
for i in xrange(1, len(words)):
74+
if len(words[i-1]) > len(words[i]) and \
75+
words[i-1][:len(words[i])] == words[i]:
76+
return ""
7177
self.findEdges(words[i - 1], words[i], ancestors)
7278

7379
# Output topological order by DFS.

0 commit comments

Comments
 (0)