Untitled
unknown
plain_text
10 months ago
660 B
2
Indexable
// @leet start /** * @param {string[]} words * @return {string[]} */ function commonChars(words) { if (words.length === 0) return []; let res = words[0].split("").sort(); for (let i = 1; i < words.length; i++) { let cur = 0; let idx = 0; if (res.length === 0) return []; const w = words[i].split("").sort(); // const w = words[i] while (cur < res.length) { const char = res[cur]; if (!w[idx]) res.splice(cur); if (char === w[idx]) { cur++; idx++; } else if (char > w[idx]) { idx++; } else { res.splice(cur, 1); } } } return res; } // @leet end
Editor is loading...
Leave a Comment