LeetCode: Word Subsets Solution
1const count = word => {2 let occ = new Int8Array(26)3 for (const c of word.split("")) {4 occ[c.charCodeAt(0) - "a".charCodeAt(0)]++5 }6 return occ7}89/**10 * @param {string[]} A11 * @param {string[]} B12 * @return {string[]}13 */14var wordSubsets = function (A, B) {15 const bMax = new Int8Array(26)16 for (const b of B) {17 const bCount = count(b)18 for (let i = 0; i < 26; i++) {19 bMax[i] = Math.max(bMax[i], bCount[i])20 }21 }2223 const res = []24 aIteration: for (const a of A) {25 const aCount = count(a)26 for (let i = 0; i < 26; i++) {27 if (aCount[i] < bMax[i]) {28 continue aIteration29 }30 }31 res.push(a)32 }3334 return res35}
Comments
Loading comments...
Tags
leetcode
string
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.