forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-word-in-dictionary-through-deleting.py
43 lines (41 loc) · 1.17 KB
/
longest-word-in-dictionary-through-deleting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Time: O((d * l) * logd), l is the average length of words
# Space: O(1)
# Given a string and a string dictionary,
# find the longest string in the dictionary
# that can be formed by deleting some characters of the given string.
# If there are more than one possible results,
# return the longest word with the smallest lexicographical order.
# If there is no possible result, return the empty string.
#
# Example 1:
# Input:
# s = "abpcplea", d = ["ale","apple","monkey","plea"]
#
# Output:
# "apple"
# Example 2:
# Input:
# s = "abpcplea", d = ["a","b","c"]
#
# Output:
# "a"
# Note:
# All the strings in the input will only contain lower-case letters.
# The size of the dictionary won't exceed 1,000.
# The length of all the strings in the input won't exceed 1,000.
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 ""