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 === 0
10}
11
12/**
13 * @param {string} s
14 * @param {string[]} d
15 * @return {string}
16 */
17var findLongestWord = function (s, d) {
18 let max = 0
19 let maxWord = ""
20 d = d.sort()
21 for (const word of d) {
22 if (isSubset(word, s) && word.length > max) {
23 max = word.length
24 maxWord = word
25 }
26 }
27
28 return maxWord
29}

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

HoningJS

Search Posts