LeetCode: Longest Word In Dictionary Through Deleting Solution
1const isSubset = (s1, s2) => {2 s1 = s1.split("")3 s2 = s2.split("")4 while (s2.length) {5 if (s2.pop() === s1.slice(-1)[0]) {6 s1.pop()7 }8 }9 return s1.length === 010}1112/**13 * @param {string} s14 * @param {string[]} d15 * @return {string}16 */17var findLongestWord = function (s, d) {18 let max = 019 let maxWord = ""20 d = d.sort()21 for (const word of d) {22 if (isSubset(word, s) && word.length > max) {23 max = word.length24 maxWord = word25 }26 }2728 return maxWord29}
Comments
Loading comments...
Tags
leetcode
two pointers
sorting
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Search A 2d Matrix II
Feb 23, 2021
Previous Post
LeetCode: Roman To Integer
Feb 21, 2021