解:
思路:遍历dictionary ,再使用双指针,挨个比较成员和s。然后将长度最长的存储下来
let s = "abce", dictionary = ["abe", "abc"]
var findLongestWord = function (s, dictionary) {
let res = ''
dictionary.forEach(val => {
let len = s.length - 1, len1 = val.length - 1
while (len >= 0) {
if (s[len] === val[len1]) {
len1--;
len--;
} else {
len--
}
}
if (len1 === -1) {
if ((res.length < val.length || (val.length === res.length && val < res))) {
res = val
}
}
})
return res
};
console.log(findLongestWord(s, dictionary));