524. Longest Word in Dictionary through Deleting
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Array, Two Pointers, String, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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 ""
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions