-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0953_Verifying_an_Alien_Dictionary.py
More file actions
28 lines (23 loc) · 1.01 KB
/
0953_Verifying_an_Alien_Dictionary.py
File metadata and controls
28 lines (23 loc) · 1.01 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
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
order_dict_letter_index = {}
for index, each_order in enumerate(order):
order_dict_letter_index[each_order] = index
if len(words) == 1:
return True
pre_words = words[0]
for each_words in words[1:]:
index = 0
min_len = min(len(pre_words), len(each_words))
isInOrder = False
while index < min_len:
if order_dict_letter_index[pre_words[index]] > order_dict_letter_index[each_words[index]]:
return False
elif order_dict_letter_index[pre_words[index]] < order_dict_letter_index[each_words[index]]:
isInOrder = True
break
index += 1
if not isInOrder and len(pre_words) > len(each_words):
return False
pre_words = each_words
return True