-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_953.py
More file actions
32 lines (26 loc) · 1.08 KB
/
task_953.py
File metadata and controls
32 lines (26 loc) · 1.08 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
from typing import List
from collections import Counter
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
def more(s, t):
ns, nt = len(s), len(t)
equal = True
for i in range(min(ns, nt)):
if idx[s[i]] > idx[t[i]]:
return True
elif idx[s[i]] < idx[t[i]]:
return False
return ns >= nt
idx = Counter()
for i, c in enumerate(order):
idx[c] = i
n = len(words)
for i in range(n):
for j in range(i+1, n):
# print(f'{words[i], words[j], more(words[i], words[j])}')
if more(words[i], words[j]):
return False
return True
print(Solution().isAlienSorted(words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"))
print(Solution().isAlienSorted(words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"))
print(Solution().isAlienSorted(words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"))