-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalien_dict.py
47 lines (30 loc) · 1.39 KB
/
alien_dict.py
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
from typing import List
'''
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
8:03 = 8 mins into the prompt. Can't figure out the brute force. Looks like a trick question.
9:00 = Interesting solution. I was off from the right track.
14:00 = Read it thoroughly for 5-10 mins. Try to reproduce on my own now.
29:00 = Looked up documentation for using "else" with "for" loop in python language.
35:00 = Done. Leetcode passed.
'''
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
order_indexes = {char: index for index, char in enumerate(order)}
for pivot in range(len(words)-1):
curr = words[pivot]
nxt = words[pivot+1]
for i in range(min(len(curr), len(nxt))):
# verify order on first distinct character seen
if curr[i] != nxt[i]:
if order_indexes[curr[i]] > order_indexes[nxt[i]]:
return False
break
# for loop completed with iteration
# the case is such that curr = "face" and nxt = "facebook"
# verify length of the words
else:
if len(curr) > len(nxt):
return False
return True