Skip to content

Commit 31d2d4e

Browse files
committed
feat: 通过删除字母匹配到字典里最长单词
1 parent 6f10569 commit 31d2d4e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} d
4+
* @return {string}
5+
*/
6+
let findLongestWord = function (s, d) {
7+
let n = d.length
8+
let points = Array(n).fill(-1)
9+
10+
let find = ""
11+
for (let i = 0; i < s.length; i++) {
12+
let char = s[i]
13+
for (let j = 0; j < n; j++) {
14+
let targetChar = d[j][points[j] + 1]
15+
if (char === targetChar) {
16+
points[j]++
17+
let word = d[j]
18+
let wl = d[j].length
19+
if (points[j] === wl - 1) {
20+
let fl = find.length
21+
if (wl > fl || (wl === fl && word < find)) {
22+
find = word
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
return find
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy