Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created December 28, 2019 02:31
Show Gist options
  • Save luoheng23/caafac95df18bd1d955afe16312eb9be to your computer and use it in GitHub Desktop.
Save luoheng23/caafac95df18bd1d955afe16312eb9be to your computer and use it in GitHub Desktop.
class Solution:
def longestWord(self, words: List[str]) -> str:
"""
:type words: List[str]
:rtype: str
"""
words.sort()
save = set() #用list存储的话,后面搜索会比较慢
res = ""
for word in words:
if word[:-1] in save or word[:-1] == '': #单字母的单词 word[:-1] == ''
if len(word) > len(res):
res = word
save.add(word)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment