-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path953.py
49 lines (38 loc) · 1.7 KB
/
953.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
48
49
'''
953. Verifying an Alien Dictionary
https://leetcode.com/problems/verifying-an-alien-dictionary/
Hi, here's your problem today. This problem was recently asked by Apple:
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library’s sort function for this problem.
Can you do this in a single pass?
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
'''
from typing import *
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
order_index = {c: i for i, c in enumerate(order)}
for i in range(len(words) - 1):
word1 = words[i]
word2 = words[i+1]
# Find the first difference word1[k] != word2[k].
for k in range(min(len(word1), len(word2))):
# If they compare badly, it's not sorted.
if word1[k] != word2[k]:
if order_index[word1[k]] > order_index[word2[k]]:
return False
break
else:
# If we didn't find a first difference, the
# words are like ("app", "apple").
if len(word1) > len(word2):
return False
return True
print(Solution().isAlienSorted(["hello","leetcode"], "hlabcdefgijkmnopqrstuvwxyz"))
#True
print(Solution().isAlienSorted(["word","world","row"], "worldabcefghijkmnpqstuvxyz"))
#False
print(Solution().isAlienSorted(["apple","app"], "abcdefghijklmnopqrstuvwxyz"))
#False