/** * @param {string[]}words * @return {string} */ var longestWord = function (words) { let max = 0; let res = ""; const set = newSet(); words.sort((a, b) => a.length - b.length); const isExist = (word) => { for (let i = 1; i <= word.length; i++) { const str = word.slice(0, i); if (!set.has(str)) returnfalse; } returntrue; }; for (const word of words) { set.add(word); if ( (word.length > max || (word.length === max && word < res)) && isExist(word) ) { res = word; max = word.length; } } return res; };