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"
  1. All the strings in the input will only contain lower-case letters.
  2. The size of the dictionary won’t exceed 1,000.
  3. The length of all the strings in the input won’t exceed 1,000.
def findLongestWord(self, s, d):
“””
:type s: str
:type d: List[str]
:rtype: str
“””
result = []
for each in sorted(d, key = lambda x: (-len(x), x)):
i, j = 0, 0

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

return “”
class Solution(object):
def findLongestWord(self, s, d):
“””
:type s: str
:type d: List[str]
:rtype: str
“””
result = []
for word in sorted(d, key = lambda x: (-len(x), x)):
it = iter(s)
if all(char in it for char in word):
return word

return “”

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JavaScript Interview Preparation (Part-2)

Reusable Presentational Components in Angular 6+

Technologies We’ve Decided To Focus On

What are PropTypes and why do we use them

React JS In 2 Minutes

Why React?

Angular Universal: ReferenceError: window is not defined

MERN app with friends or ChocolateyAutoInstallMakefileFun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Newbie Developer

Newbie Developer

More from Medium

Simple and Multiple Linear Regression

Bias in Algorithm

Chi-Square Test in Python: Analyzing Enabling Factors of Mental Health at Work

What Is Regression In Data Mining?