Untitled

 avatar
unknown
plain_text
a year ago
930 B
5
Indexable
1002
function commonChars(words: string[]): string[] {
    if (words.length === 0) return [];

    let res = words[0].split('').sort();

    for (let i = 1; i < words.length; i++) {
        console.log({ res, word: words[i]})

        let cur = 0;
        let idx = 0
        if (res.length === 0 || cur >= res.length) return []

        const w = words[i].split('').sort();
        // const w = words[i]
        console.log({ res, w, cr: w[idx] })

        while (cur < res.length && idx < w.length) {
            const char = res[cur]
            console.log({ char, currentWord: w, widx: w[idx] })

            if (char === w[idx]) {
                cur++
                idx++
            } else if (char > w[idx]) {
                idx++
            } else if (char < w[idx]) {
                res.splice(cur, 1);
                console.log({ res })

                // cur++
            }
        }
    }

    return res
};
Editor is loading...
Leave a Comment