-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path374.py
More file actions
19 lines (16 loc) · 709 Bytes
/
374.py
File metadata and controls
19 lines (16 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
def compare(word_a, word_b, alphabet):
for i in range(min(len(word_a), len(word_b))):
if alphabet[word_a[i]] < alphabet[word_b[i]]:
return 0
elif alphabet[word_a[i]] > alphabet[word_b[i]]:
return 1
return 0 if len(word_a) <= len(word_b) else 1
alphabet = {ch: i for i, ch in enumerate(order)}
for i in range(len(words) - 1):
word_a = words[i]
word_b = words[i+1]
if compare(word_a, word_b, alphabet) == 1:
return False
return True