Skip to content

Commit 96407d9

Browse files
authored
Merge pull request #1742 from AP-Repositories/patch-25
Create 0953-verifying-an-alien-dictionary.py
2 parents 1321328 + 640a988 commit 96407d9

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)