Word Subsets

MediumStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Say string b *fits inside* string a if every letter occurs in a at least as many times as it occurs in b (so "oo" fits inside "foo" but not inside "fob"). Given two string arrays words1 and words2, a word a in words1 is *universal* if every string in words2 fits inside it. Return all universal words, in the same order they appear in words1.

Examples

Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]
Only these three words contain both an "e" and an "o".
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]
These words contain at least one "l" and one "e".

Constraints

  • `1 <= words1.length, words2.length <= 10^4`
  • `1 <= words1[i].length, words2[i].length <= 10`
  • All strings are lowercase English letters
  • All strings in `words1` are unique

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems