Leetcode 524 Solution

This article provides solution to leetcode question 524 (longest-word-in-dictionary-through-deleting)

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting

Solution

class Solution(object):
    def findLongestWord(self, s, d):
        """
        :type s: str
        :type d: List[str]
        :rtype: str
        """
        words = sorted([
            (-len(word), word)
            for word in d
        ])

        for _, word in words:
            i = 0
            j = 0

            while i < len(s) and j < len(word):
                if s[i] == word[j]:
                    j += 1
                i += 1

            if j == len(word):
                return word

        return ""