Word Subsets

Medium Topics Company Tags

Return every string in words1 that contains all the letters required by each individual string in words2. Letter order does not matter, but repeated letters do: a requirement of "aa" needs at least two occurrences of a. Check each requirement independently, so different strings in words2 can reuse the same occurrences in a candidate. Return the qualifying strings in any order.

Example 1:

Input: words1 = ["cat","taco","coat","dog"]
words2 = ["at","c"]

Output: ["cat","coat","taco"]

Explanation: "cat", "taco", and "coat" each contain a, t, and c. "dog" does not satisfy either requirement.

Example 2:

Input: words1 = ["ab","aab","aba","aa"]
words2 = ["aa","b"]

Output: ["aab","aba"]

Explanation: The requirements together need two copies of a and one copy of b. Only "aab" and "aba" have enough of both letters.

Example 3:

Input: words1 = ["abc","ab"]
words2 = ["ab","ab"]

Output: ["ab","abc"]

Explanation: Each occurrence of the requirement "ab" is checked independently. Both candidates contain a and b, so repeating the requirement does not require extra copies.

Constraints:

  • 1 <= words1.length, words2.length <= 10000
  • Each word contains between 1 and 10 lowercase English letters.
  • All strings in words1 are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.