Verifying an Alien Dictionary

字典排序:理解错了 以为每个单词都要排序 那就不用isWordOk 那个def了

class Solution(object):
    def isAlienSorted(self, words, order):
    	dict = {}
    	for index in range(len(order)):
    		dict[order[index]] = index
    	if len(words) <= 1:
    		return True
    	for index in range(len(words)-1):
    		if self.isTwoOk(words[index], words[index+1], dict):#self.isWordOk(words[index], dict) and self.isWordOk(words[index+1], dict) and 
    			continue
    		else:
    			return False
    	return True

    def isWordOk(self, words, dict):
    	if len(words) <= 1:
    		return True
    	for index in range(1,len(words)):
    		if  words[index] in dict:
    			if dict[words[index]] < dict[words[index-1]]:
    				return False
    		else:
    			return False
    	return True
    def isTwoOk(self, words, word2, dict):
    	for index in range(min(len(words), len(word2))):
    		if dict[word2[index]] > dict[words[index]]: 
    			return True
    		elif dict[word2[index]] < dict[words[index]]:
    			return False 
    	if len(words) > len(word2):
    		return False
    	return True
a = "abcdefghijklmnopqrstuvwxyz"
b = ["apple","app"]
t = Solution()
print(t.isAlienSorted(b, a))

猜你喜欢

转载自blog.csdn.net/m0_37770463/article/details/88686620
今日推荐