Skip to content

Commit 640a988

Browse files
Create 0953-verifying-an-alien-dictionary.py
code transcribed from the video https://www.youtube.com/watch?v=OVgPAJIyX6o
1 parent 2d0e0cc commit 640a988

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isAlienSorted(self, words: List[str], order: str) -> bool:
3+
# first differing char
4+
# if word A is prefix of word B, word B must be AFTER word A
5+
orderInd = { c : i for i, c in enumerate(order)}
6+
7+
for i in range(len(words) - 1):
8+
w1, w2 = words[i], words[i + 1]
9+
10+
for j in range(len(w1)):
11+
if j == len(w2):
12+
return False
13+
14+
if w1[j] != w2[j]:
15+
if orderInd[w2[j]] < orderInd[w1[j]]:
16+
return False
17+
break
18+
return True

0 commit comments

Comments
 (0)