Skip to content

Commit b3394c8

Browse files
authored
Update 524-longest-word-in-dictionary-through-deleting.js
1 parent 39fa4f4 commit b3394c8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

524-longest-word-in-dictionary-through-deleting.js

+32
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@
1919
return res
2020
};
2121

22+
// another
23+
24+
/**
25+
* @param {string} s
26+
* @param {string[]} dictionary
27+
* @return {string}
28+
*/
29+
const findLongestWord = function (s, dictionary) {
30+
const n = dictionary.length
31+
const idxArr = Array(n).fill(0)
32+
let res = ''
33+
for (const ch of s) {
34+
for (let i = 0; i < n; i++) {
35+
const idx = idxArr[i]
36+
if (idx >= dictionary[i].length) continue
37+
if (ch === dictionary[i][idx]) {
38+
idxArr[i]++
39+
}
40+
41+
if (
42+
idxArr[i] === dictionary[i].length &&
43+
(dictionary[i].length > res.length ||
44+
(dictionary[i].length === res.length && dictionary[i] < res))
45+
) {
46+
res = dictionary[i]
47+
}
48+
}
49+
}
50+
return res
51+
}
52+
53+
2254
// another
2355

2456
/**

0 commit comments

Comments
 (0)