LeetCode_哈希表_中等_916.单词子集

Bertha 。 2023-10-07 19:54 142阅读 0赞

目录

  • 1.题目
  • 2.思路
  • 3.代码实现(Java)

1.题目

给你两个字符串数组 words1 和 words2。

现在,如果 b 中的每个字母都出现在 a 中,包括重复出现的字母,那么称字符串 b 是字符串 a 的子集

例如,“wrr” 是 “warrior” 的子集,但不是 “world” 的子集。
如果对 words2 中的每一个单词 b,b 都是 a 的子集,那么我们称 words1 中的单词 a 是通用单词

以数组形式返回 words1 中所有的通用单词。你可以按任意顺序返回答案。

示例 1:
输入:words1 = [“amazon”,“apple”,“facebook”,“google”,“leetcode”], words2 = [“e”,“o”]
输出:[“facebook”,“google”,“leetcode”]

示例 2:
输入:words1 = [“amazon”,“apple”,“facebook”,“google”,“leetcode”], words2 = [“l”,“e”]
输出:[“apple”,“google”,“leetcode”]

示例 3:
输入:words1 = [“amazon”,“apple”,“facebook”,“google”,“leetcode”], words2 = [“e”,“oo”]
输出:[“facebook”,“google”]

示例 4:
输入:words1 = [“amazon”,“apple”,“facebook”,“google”,“leetcode”], words2 = [“lo”,“eo”]
输出:[“google”,“leetcode”]

示例 5:
输入:words1 = [“amazon”,“apple”,“facebook”,“google”,“leetcode”], words2 = [“ec”,“oc”,“ceo”]
输出:[“facebook”,“leetcode”]

提示:
1 <= words1.length, words2.length <= 104
1 <= words1[i].length, words2[i].length <= 10
words1[i] 和 words2[i] 仅由小写英文字母组成
words1 中的所有字符串互不相同

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-subsets

2.思路

(1)暴力穷举法
暴力穷举法比较容易想到,即使用 2 层 for 循环来进行判断(在此之前还可以先对 words2 中的单词进行去重处理),但是该方法的时间复杂度较高,在 LeetCode 中提交时会出现“超出时间限制”的提示!

(2)将 words2 合并成一个单词
思路参考本题官方题解。

3.代码实现(Java)

  1. //思路1————暴力穷举法
  2. class Solution {
  3. public List<String> wordSubsets(String[] words1, String[] words2) {
  4. Set<String> hashSet = new HashSet<>(Arrays.asList(words2));
  5. List<String> res = new ArrayList<>();
  6. int len2 = hashSet.size();
  7. for (String a : words1) {
  8. int cnt = 0;
  9. for (String b : hashSet) {
  10. if (isSubset(a, b)) {
  11. cnt++;
  12. } else {
  13. break;
  14. }
  15. }
  16. if (cnt == len2) {
  17. res.add(a);
  18. }
  19. }
  20. return res;
  21. }
  22. //判断 b 是否为 a 的子集,如果是则返回 true,否则返回 false
  23. public boolean isSubset(String a, String b) {
  24. char[] ach = a.toCharArray();
  25. char[] bch = b.toCharArray();
  26. Map<Character, Integer> hashMap = new HashMap<>();
  27. for (char c : ach) {
  28. hashMap.put(c, hashMap.getOrDefault(c, 0) + 1);
  29. }
  30. for (char c : bch) {
  31. if (hashMap.containsKey(c) && hashMap.get(c) >= 1) {
  32. hashMap.put(c, hashMap.get(c) - 1);
  33. } else {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. }
  40. //思路2————将 words2 合并成一个单词
  41. class Solution {
  42. public List<String> wordSubsets(String[] words1, String[] words2) {
  43. // words2Max 保存 words2 中字符串的每个字符出现的最大次数
  44. int[] words2Max = count("");
  45. for (String b : words2) {
  46. int[] bCount = count(b);
  47. for (int i = 0; i < 26; i++) {
  48. words2Max[i] = Math.max(words2Max[i], bCount[i]);
  49. }
  50. }
  51. List<String> res = new ArrayList<>();
  52. for (String a : words1) {
  53. boolean flag = true;
  54. int[] aCount = count(a);
  55. for (int i = 0; i < 26; i++) {
  56. if (aCount[i] < words2Max[i]) {
  57. flag = false;
  58. break;
  59. }
  60. }
  61. if (flag) {
  62. res.add(a);
  63. }
  64. }
  65. return res;
  66. }
  67. public int[] count(String s) {
  68. int[] res = new int[26];
  69. for (char c : s.toCharArray()) {
  70. res[c - 'a']++;
  71. }
  72. return res;
  73. }
  74. }

发表评论

表情:
评论列表 (有 0 条评论,142人围观)

还没有评论,来说两句吧...

相关阅读

    相关 leetcode 916 单词子集

    我们给出两个单词数组 A 和 B。每个单词都是一串小写字母。 现在,如果 b 中的每个字母都出现在 a 中,包括重复出现的字母,那么称单词 b 是单词 a 的子集。 例如,“

    相关 OPEN实现单词

    一、算法思想 哈希表是根据关键码值而直接进行访问的数据结构,也就是说它可以通过对关键字进行某种函数映射直接找到其对应的表中的位置,这个映射函数叫做哈希函数。 由于哈希表中不