【每日一题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)

相关推荐
格林威5 小时前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
程序员莫小特7 小时前
老题新解|大整数加法
数据结构·c++·算法
过往入尘土8 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
zycoder.8 小时前
力扣面试经典150题day1第一题(lc88),第二题(lc27)
算法·leetcode·面试
Dream it possible!9 小时前
LeetCode 面试经典 150_哈希表_存在重复元素 II(46_219_C++_简单)
leetcode·面试·散列表
蒙奇D索大9 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it
智驱力人工智能9 小时前
工厂抽烟检测系统 智能化安全管控新方案 加油站吸烟检测技术 吸烟行为智能监测
人工智能·算法·安全·边缘计算·抽烟检测算法·工厂抽烟检测系统·吸烟监测
学学学无无止境9 小时前
组合两个表-力扣
leetcode
程序员爱钓鱼10 小时前
Go语言实战案例——进阶与部署篇:编写Makefile自动构建Go项目
后端·算法·go
_Power_Y10 小时前
Java面试常用算法api速刷
java·算法·面试