leetcode - 916. Word Subsets

Description

You are given two string arrays words1 and words2.

A string b is a subset of string a if every letter in b occurs in a including multiplicity.

For example, "wrr" is a subset of "warrior" but is not a subset of "world".

A string a from words1 is universal if for every string b in words2, b is a subset of a.

Return an array of all the universal strings in words1. You may return the answer in any order.

Example 1:

复制代码
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:

复制代码
Input: words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
Output: ["apple","google","leetcode"]

Constraints:

复制代码
1 <= words1.length, words2.length <= 10^4
1 <= words1[i].length, words2[i].length <= 10
words1[i] and words2[i] consist only of lowercase English letters.
All the strings of words1 are unique.

Solution

A little bit tricky and one take-away is: if the total time complexity is around 1 0 8 10^8 108, don't try it, it will be TLE.

The core idea is, we build a hashmap of words2, and iterate all the strings in words1 to decide if we want to select it. The tricky part is how we build the hash map.

Because oo won't be a subset of aeiou, but ['ao', 'eo'] would be a subset of aeiou, so to build the hash map, we just need to store the maximum frequency of the string in words2.

Time complexity: o ( w o r d s 2. l e n + w o r d s 1. l e n ) o(words2.len + words1.len) o(words2.len+words1.len)

Space complexity: o ( 1 ) o(1) o(1)

Code

python3 复制代码
class Solution:
    def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
        words2_fre = [0] * 26
        for each_word in words2:
            ch_cnt = collections.Counter(each_word)
            for each_ch, each_cnt in ch_cnt.items():
                ch_index = ord(each_ch) - ord('a')
                words2_fre[ch_index] = max(words2_fre[ch_index], each_cnt)
        res = []
        for each_word in words1:
            ch_cnt = collections.Counter(each_word)
            is_candidate = True
            for i in range(26):
                if ch_cnt.get(chr(i + ord('a')), 0) < words2_fre[i]:
                    is_candidate = False
                    break
            if is_candidate:
                res.append(each_word)
        return res
相关推荐
唐青枫29 分钟前
C#.NET SpinLock 深入解析:自旋锁原理、使用边界与性能取舍
c#·.net
灰色小旋风1 小时前
力扣21 合并两个有序链表(C++)
c++·leetcode·链表
老鼠只爱大米2 小时前
LeetCode经典算法面试题 #347:前 K 个高频元素(最小堆、桶排序、快速选择等多种实现方案详解)
算法·leetcode·堆排序·java面试题·桶排序·快速选择·topk
liuyao_xianhui3 小时前
优选算法_分治_快速排序_归并排序_C++
开发语言·数据结构·c++·算法·leetcode·排序算法·动态规划
sheeta19985 小时前
LeetCode 每日一题笔记 日期:2025.03.23 题目:1594.矩阵的最大非负积
笔记·leetcode·矩阵
灰色小旋风5 小时前
力扣22 括号生成(C++)
开发语言·数据结构·c++·算法·leetcode
圣保罗的大教堂5 小时前
leetcode 1594. 矩阵的最大非负积 中等
leetcode
重生之我是Java开发战士6 小时前
【广度优先搜索】队列:N叉树的层序遍历,二叉树的锯齿形层序遍历,二叉树的最大宽度,在每个树行中找最大值
数据结构·算法·leetcode·广度优先
灰色小旋风6 小时前
力扣20有效的括号(C++)
c++·算法·leetcode·职场和发展
逆境不可逃7 小时前
LeetCode 热题 100 之 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表 142. 环形链表 II
算法·leetcode·链表