File size: 1,761 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10031", "name": "LeetCode 0524: Longest Word In Dictionary Through Deleting"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Longest Word in Dictionary through Deleting (Difficulty: Medium)\nTags: Array, Two Pointers, String, Sorting\n\nGiven a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.\n\nExample 1:\nInput: s = \"abpcplea\", dictionary = [\"ale\",\"apple\",\"monkey\",\"plea\"]\nOutput: \"apple\"\n\nExample 2:\nInput: s = \"abpcplea\", dictionary = [\"a\",\"b\",\"c\"]\nOutput: \"a\"\n\nConstraints:\n- 1 <= s.length <= 1000\n- 1 <= dictionary.length <= 1000\n- 1 <= dictionary[i].length <= 1000\n- s and dictionary[i] consist of lowercase English letters."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O((d * l) * logd), l is the average length of words\n# Space: O(1)\n\nclass Solution(object):\n    def findLongestWord(self, s, d):\n        \"\"\"\n        :type s: str\n        :type d: List[str]\n        :rtype: str\n        \"\"\"\n        d.sort(key = lambda x: (-len(x), x))\n        for word in d:\n            i = 0\n            for c in s:\n                if i < len(word) and word[i] == c:\n                    i += 1\n            if i == len(word):\n                return word\n        return \"\"\n\n"}}