-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path524.linningmii.py
38 lines (29 loc) · 1.05 KB
/
524.linningmii.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
class Solution:
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
result = ""
for word in d:
if self.include_extend(s, word):
if len(word) > len(result):
result = word
elif len(word) == len(result) and word < result:
result = word
return result
def include_extend(self, outer, inner):
inner_pointer = 0
result_str = ""
for i in range(len(outer)):
if inner_pointer == len(inner):
return result_str == inner
if outer[i] == inner[inner_pointer]:
result_str += inner[inner_pointer]
inner_pointer += 1
return result_str == inner
solution = Solution()
print(solution.findLongestWord("abpcplea", ["ale", "apple", "monkey", "plea"])) # apple
print(solution.findLongestWord("abpcplea", ["a", "b", "c"])) # a
print(solution.findLongestWord("bab", ["ba", "ab", "a", "b"])) # ab