【每日一题Day370】LC318最大单词长度乘积 | 哈希表 位运算

最大单词长度乘积【LC318】

给你一个字符串数组 words ,找出并返回 length(words[i]) * length(words[j]) 的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0

2022/10/17

位运算

  1. 将每个单词转化为整数(二进制形式),用最低位代表a,第二低位代表b......最高位(第二十六位)代表z,例如单词"ab"可表示为11,十进制为3
  2. 将某个单词的二进制形式与其他单词进行与运算,如果结果为0,那么代表这两个单词不含有公共字母,判断是否需要更新结果
  • 代码

    java 复制代码
    class Solution {
        public int maxProduct(String[] words) {
            int len = words.length;
            int[] binary = new int[len]; // a:1 b:2 c:4 ......
            for (int i = 0; i < len; i++){
                String str = words[i];
                boolean[] map = new boolean[26];
                for (int j = 0; j < str.length(); j++){
                    int n = str.charAt(j)-'a';
                    if (!map[n]){
                        binary[i] += Math.pow(2,n);
                        map[n] = true;
                    }
                }
            }
            int res = 0;
            for (int i = 0; i < len; i++){
                for (int j = i + 1; j < len; j++){
                    if ((binary[i] & binary[j]) == 0){
                        res = Math.max(res,words[i].length()*words[j].length());
                    }
                }
            }    
            return res;    
        }
    }
  • 复杂度

    • 时间复杂度:O(n2+nk),n为字符串的个数,k为每个字符串的平均长度

    • 空间复杂度:O(n)

  • 优化

    将每个字符串转化为二进制的代码可优化为

    java 复制代码
    for (int i = 0; i < len; i++){
         String str = words[i];
         for (char ch: words[i].toCharArray()){
            binary[i] |= 1 << (ch - 'a');	
        }
    }

哈希表

使用哈希表记录出现在该字符串的所有字符

  • 代码

    java 复制代码
    class Solution {
        public int maxProduct(String[] words) {
            int len = words.length;
            boolean[][] flags = new boolean[len][26]; 
            for (int i = 0; i < len; i++){
                String str = words[i];
                for (char ch: words[i].toCharArray()){
                    flags[i][ch-'a'] = true;
                }
            }
            int res = 0;
            for (int i = 0; i < len; i++){
                for (int j = i + 1; j < len; j++){
                    int k = 0;
                    for (; k < 26; k++){
                        if (flags[i][k] && flags[j][k]){
                            break;
                        }
                    }
                    if (k == 26){
                        res = Math.max(res,words[i].length()*words[j].length());
                    }
                }
            }    
            return res;    
        }
    }
  • 复杂度

    • 时间复杂度:O(n2+nk),n为字符串的个数,k为每个字符串的平均长度

    • 空间复杂度:O(n)

相关推荐
Dream it possible!3 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
?abc!4 小时前
缓存(5):常见 缓存数据淘汰算法/缓存清空策略
java·算法·缓存
BioRunYiXue4 小时前
一文了解氨基酸的分类、代谢和应用
人工智能·深度学习·算法·机器学习·分类·数据挖掘·代谢组学
jiunian_cn5 小时前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
工藤新一¹6 小时前
蓝桥杯算法题 -蛇形矩阵(方向向量)
c++·算法·矩阵·蓝桥杯·方向向量
Levin__NLP_CV_AIGC6 小时前
解决pip安装PyPI默认源速度慢
算法·pip
Helibo447 小时前
GESPC++六级复习
java·数据结构·算法
EnticE1527 小时前
[高阶数据结构]二叉树经典面试题
数据结构·算法·面试
MarkHard1238 小时前
Leetcode (力扣)做题记录 hot100(34,215,912,121)
算法·leetcode·职场和发展
爱喝茶的小茶8 小时前
构造+简单树状
数据结构·算法