Skip to content

Commit a3b4a53

Browse files
authored
Create verifying-an-alien-dictionary.py
1 parent f3bbad2 commit a3b4a53

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n * l), l is the average length of words
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def isAlienSorted(self, words, order):
6+
"""
7+
:type words: List[str]
8+
:type order: str
9+
:rtype: bool
10+
"""
11+
lookup = {c: i for i, c in enumerate(order)}
12+
for i in xrange(len(words)-1):
13+
word1 = words[i]
14+
word2 = words[i+1]
15+
for k in xrange(min(len(word1), len(word2))):
16+
if word1[k] != word2[k]:
17+
if lookup[word1[k]] > lookup[word2[k]]:
18+
return False
19+
break
20+
else:
21+
if len(word1) > len(word2):
22+
return False
23+
return True

0 commit comments

Comments
 (0)