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 occ
7}
8
9/**
10 * @param {string[]} A
11 * @param {string[]} B
12 * @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 }
22
23 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 aIteration
29 }
30 }
31 res.push(a)
32 }
33
34 return res
35}

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.

Next Post

LeetCode: Palindromic Substrings

HoningJS

Search Posts