Medium
Longest Word in Dictionary through Deleting — Python
Full explanation · Time O((d * l) * logd) · Space O(1)
# Time: O((d * l) * logd), l is the average length of words
# Space: O(1)
class Solution(object):
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
d.sort(key = lambda x: (-len(x), x))
for word in d:
i = 0
for c in s:
if i < len(word) and word[i] == c:
i += 1
if i == len(word):
return word
return ""