-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.py
76 lines (56 loc) · 2.29 KB
/
Dictionary.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# english lowercase letter. order is permutation of letter.
# given a sequence of words and order of alphabet. Return true if sorted lexigraphically
# input = ["hello", "leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"" = output true
# words = ["apple","app"], order = "abcdefghijklmnop qrstuvwxyz"
# verifying alien dictionairy.
# at each words index[i] if it doesnt exist from string[:]
# words should be in alphabetical order
#
# Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
# Output: false
# Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
def alienDict(inputStr, order):
for i in range(len(inputStr) - 1):
wordOne = inputStr[i]
wordTwo = inputStr[i + 1]
minLength = min(len(wordOne), len(wordTwo))
for j in range(minLength):
# print(wordOne[j], wordTwo[j])
# print(order.index(wordOne[j]), order.index(wordTwo[j]))
# print(order.index(wordOne[j]) > order.index(wordTwo[j]))
if wordOne[j] == wordTwo[j]:
continue
elif order.index(wordOne[j]) > order.index(wordTwo[j]):
return False
else:
break
if len(wordOne) > len(wordTwo):
return False
return True
arr = ["word", "world", "row"]
order = "worldabcefghijkmnpqstuvxyz"
arr2 = ["hello", "leetcode"]
order2 = "hlabcdefgijkmnopqrstuvwxyz"
print(alienDict(arr2, order2))
print(alienDict(arr, order))
["kuvp", "q"]
"ngxlkthsjuoqcpavbfdermiywz" = > true
# def isAlienSorted(self, words, order):
# for i in range(len(words) - 1):
# current = words[i]
# next_word = words[i + 1]
# idx = 0
# while idx < len(current):
# left = current[idx] if idx < len(current) else None
# right = next_word[idx] if idx < len(next_word) else None
# if not right:
# return False
# leftIdx = order.index(left)
# rightIdx = order.index(right)
# if leftIdx == rightIdx:
# idx += 1
# elif leftIdx < rightIdx:
# break
# else:
# return False
# return True