Brute Force(暴力)查找即可.

注意Python的元组也是可以比大小的,就是先比第一个元素,再第二个...

所以排序时key先按len排,len一样再按字典序排:就是key返回元组.

class Solution(object):
    def longestWord(self, words):
        words.sort(key=lambda x: (-len(x), x))
        words_set = set(words)
        for w in words:
            if all(w[:i] in words_set for i in range(1, len(w))):
                return w
        return ''

 

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
猜你喜欢
  • 2022-02-08
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2021-06-01
  • 2021-09-20
相关资源
相似解决方案