leetcode - 916. Word Subsets

ops/2025/1/14 18:49:05/

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

word">class Solution:word">def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:words2_fre = [0] * 26word">for each_word word">in words2:ch_cnt = collections.Counter(each_word)word">for each_ch, each_cnt word">in ch_cnt.items():ch_index = ord(each_ch) - ord('a')words2_fre[ch_index] = max(words2_fre[ch_index], each_cnt)res = []word">for each_word word">in words1:ch_cnt = collections.Counter(each_word)is_candidate = Trueword">for i word">in range(26):word">if ch_cnt.get(chr(i + ord('a')), 0) < words2_fre[i]:is_candidate = Falseword">breakword">if is_candidate:res.append(each_word)word">return res

http://www.ppmy.cn/ops/150074.html

相关文章

如何将 sqlserver 数据迁移到 mysql

文章目录 前言一、导出SQL Server 数据二、转换数据格式为MySQL兼容格式三、导入数据到MySQL数据库五、使用ETL工具六、通过 navicat 工具七、总结 前言 将 SQL Server 数据迁移到 MySQL 是一个常见的数据库迁移任务&#xff0c;通常涉及以下几个关键步骤&#xff1a;导出 SQL…

Pycharm 使用教程

一、基本配置 1. 切换Python解释器 pycharm切换解释器版本 2. pycharm虚拟环境配置 虚拟环境的目的&#xff1a;创建适用于该项目的环境&#xff0c;与系统环境隔离&#xff0c;防止污染系统环境&#xff08;包括需要的库&#xff09;虚拟环境配置存放在项目根目录下的 ven…

鸿蒙面试 2025-01-11

ArkTs 和TS的关系&#xff1f; ArkTS&#xff08;方舟开发语言&#xff09;与 TypeScript&#xff08;TS&#xff09;存在紧密联系&#xff0c;同时也有显著区别&#xff1a; 联系 语法基础&#xff1a;ArkTS 在语法层面大量借鉴了 TypeScript &#xff0c;TypeScript 里诸如…

蓝桥杯历届真题 #食堂(C++,Java)

这题没什么好说的 考虑所有情况然后写就完了 虽然赛场上 交完不知道答案(doge) 原题链接 #include<iostream>using namespace std;int main() {int n;cin >> n;//能优先安排6人桌,要先安排6人桌//6人桌可以是222 或者 33 或者42//优先用33组合,因为3人寝只能凑6人…

Bash语言的语法糖

Bash语言的语法糖 引言 在现代编程语言中&#xff0c;“语法糖”是一个非常常见的术语&#xff0c;它指的是那些使代码更加易读、易写的语法特性。尽管这些特性并不改变语言的功能&#xff0c;但它们能显著提升开发者的编程体验。在众多编程语言中&#xff0c;Bash&#xff0…

Linux -- 自定义协议体会序列化和反序列化

思路介绍 网络版计算器&#xff1a; 1、客户端发送 两个操作数 和 操作符&#xff1b; 2、根据协议&#xff0c;在发送时&#xff0c;对数据进行序列化&#xff0c;再加上报头&#xff0c;形成 请求 并发送给 服务器&#xff1b; 3、服务器收到 请求 后&#xff0c;判断收到的 …

HTML实战课堂之简单的拜年程序

一、目录&#xff1a; &#xfffc;&#xfffc; 一、目录&#xff1a; 二、祝福 三&#xff1a;代码讲解 &#xff08;1&#xff09;详细解释&#xff1a; 1.HTML部分 2. CSS部分 三、运行效果&#xff08;随机截图&#xff09;&#xff1a; 四、完整代码&#xff1a; 二、祝福…

3Hive数据抽样

3Hive数据抽样 1 随机抽样2 块抽样3 桶表抽样 当数据规模不断膨胀时&#xff0c;我们需要找到一个数据的子集来加快数据分析效率。因此我们就需要通过筛选和分析数据集为了进行模式 & 趋势识别。目前来说有三种方式来进行抽样&#xff1a;随机抽样&#xff0c;桶表抽样&…