Longest Word in Dictionary through Deleting
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"Last updated
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"Last updated
class Solution:
def findLongestWord(self, s: str, d: List[str]) -> str:
ll = len(s)
maxLen,res = 0,''
for p in d:
ps=pp=0
lp = len(p)
while ps < ll and pp < lp:
if p[pp] == s[ps]:
pp += 1
ps += 1
if pp == lp:
if maxLen < lp:
maxLen = lp
res = p
elif maxLen == lp:
if res > p:
res = p
return res