720. 词典中最长的单词
为保证权益,题目请参考 720. 词典中最长的单词(From LeetCode).
解决方案1
Python
python
# 720. 词典中最长的单词
# https://leetcode-cn.com/problems/longest-word-in-dictionary/
from typing import List
class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort(key=lambda x: (-len(x), x), reverse=True)
longest = ""
candidates = {""}
for word in words:
if word[:-1] in candidates:
longest = word
candidates.add(word)
return longest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16