【每日一题20220922】外星人字典

:woman_artist: 某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。 给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。

举例

样例1:

输入:
words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:
true
解释:
在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。

样例2:

输入:
words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出:
false
解释:
在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。

样例3:

输入:
words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出:
false
解释:
当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。

题目难度:一般
题目来源:LintCode 炼码

def waixingren(words, order):
    word = []
    for i in range(len(words)):
        word.append([])

    max = 0
    for i in range(len(words)):
        if len(words[i]) > max:
            max = len(words[i])
        for j in range(len(words[i])):
            word[i].append(order.index(words[i][j]))

    for i in range(len(words)):
        if len(word[i]) < max:
            for j in range(max - len(word[i])):
                word[i].append(0)

    a = True

    for i in range(len(word)):
        for j in range(max):
            if i != 0 and word[i][j] < word[i-1][j]:
                a = False
            elif i != 0 and word[i][j] > word[i-1][j]:
                break
    return a
def words_order(words, order):
    minlenth = len(min(words))
    lresult = []
    for i in range(len(words)):
        for j in range(len(words) - i - 1):
            if len(words[j]) > len(words[j + 1]):
                return "false"
        else:
            lresult.append(words[i][0:minlenth])
    for i in range(len(lresult)):
        for j in range(len(lresult[0])):
            if order.index(lresult[i][j]) > order.index(lresult[i + 1][j]):
                return "false"
            else:
                return "true"
func IsAlienSorted(words []string, order string) bool {
	if len(words) == 1 {
		return true
	}
	hashMap := toHashMap(order)
	for i := 0; i < len(words)-1; i++ {
		var sig bool
		for j := 0; j < minLen(words[i], words[i+1]); j++ {
			if j == len(words[i]) || j == len(words[i+1]) || hashMap[words[i][j]] < hashMap[words[i+1][j]] {
				sig = true
				break
			}
			if hashMap[words[i][j]] > hashMap[words[i+1][j]] {
				return false
			}
		}
		// 解决 'words' 和 'word' 的排序问题
		if sig == false && len(words[i]) > len(words[i+1]) {
			return false
		}

	}
	return true
}
func toHashMap(order string) map[byte]int {
	hashMap := make(map[byte]int, 0)
	for i := 0; i < len(order); i++ {
		hashMap[order[i]] = i
	}
	return hashMap
}
func minLen(a, b string) int {
	if len(a) >= len(b) {
		return len(a)
	} else {
		return len(b)
	}
}
def words_order(words: list, order: str) -> bool:
    res_list=[]
    for word in words:
        word=word+'0'*(max([len(words[i]) for i in range(len(words))])-len(word))
        word_int_list=[str(order.index(str(i))+1) if i in order else i for i in word]
        res_list.append(word_int_list)
    print(res_list)
    for i in range(len(res_list)-1):
        for j in range(len(res_list[0])):
            if res_list[i][j]==res_list[i+1][j]:
                continue
            elif res_list[i][j]<res_list[i+1][j]:
                return True
            else:
                return False

assert words_order(["hello","leetcode"],"hlabcdefgijkmnopqrstuvwxyz")==True
assert words_order(["word","world","row"],"worldabcefghijkmnpqstuvxyz")==False
assert words_order(["apple","app"],"abcdefghijklmnopqrstuvwxyz")==False