题目
题意比较模糊。
优秀代码
class Solution:
def longestWord(self, words: 'List[str]') -> 'str':
res = None
maxlen = 0
look_up = set(words)
for word in words:
if len(word) >= maxlen:
if len(word) == maxlen: #相等
if word < res:
if all(word[:i] in look_up for i in range(1, len(word))):
res = word
else:
if all(word[:i] in look_up for i in range(1, len(word))):
res = word
maxlen = len(word)
return res
class Solution(object):
def longestWord(self, words):
words.sort()
words_set, longest_word = set(['']), ''
for word in words:
if word[:-1] in words_set:
words_set.add(word)
if len(word) > len(longest_word):
longest_word = word
return longest_word