-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path953_VerifyinganAlienDictionary.py
More file actions
32 lines (29 loc) · 1.44 KB
/
953_VerifyinganAlienDictionary.py
File metadata and controls
32 lines (29 loc) · 1.44 KB
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
class Solution:
def isAlienSorted(self, words, order) -> bool:
dic = {}
for i, c in enumerate(order):
dic[c] = i
for i in range(len(words)-1):
current = words[i]
next = words[i+1]
for i in range(min(len(current), len(next))):
if current[i] != next[i]:
if dic[current[i]] > dic[next[i]]:
return False
break
else:
if len(current) > len(next):
return False
return True
def main():
print(Solution().isAlienSorted(["hello","leetcode"],"hlabcdefgijkmnopqrstuvwxyz"))
# Output: true
# Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
print(Solution().isAlienSorted(["word","world","row"],"worldabcefghijkmnpqstuvxyz"))
# Output: false
# Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
print(Solution().isAlienSorted(["apple","app"],"abcdefghijklmnopqrstuvwxyz"))
# Output: false
# Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
if __name__ == '__main__':
main()