Simple JavaScript Solution


  • 0
    const longestWord = (words) => {
        words.sort()
        const wordSet = new Set(words)
        let res = ''
        for (const w of words) {
            let isValid = true
            let key = ''
            for (let i = 0; i < w.length - 1; i++) {
                key += w[i]
                if (!wordSet.has(key)) {
                    isValid = false
                    break;
                }
            }
            if (isValid && w.length > res.length) {
                res = w
            }
        }
        return res
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.