Java | Leetcode Java题解之第318题最大单词长度乘积

题目:

题解:

java 复制代码
class Solution {
    public int maxProduct(String[] words) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int length = words.length;
        for (int i = 0; i < length; i++) {
            int mask = 0;
            String word = words[i];
            int wordLength = word.length();
            for (int j = 0; j < wordLength; j++) {
                mask |= 1 << (word.charAt(j) - 'a');
            }
            if (wordLength > map.getOrDefault(mask, 0)) {
                map.put(mask, wordLength);
            }
        }
        int maxProd = 0;
        Set<Integer> maskSet = map.keySet();
        for (int mask1 : maskSet) {
            int wordLength1 = map.get(mask1);
            for (int mask2 : maskSet) {
                if ((mask1 & mask2) == 0) {
                    int wordLength2 = map.get(mask2);
                    maxProd = Math.max(maxProd, wordLength1 * wordLength2);
                }
            }
        }
        return maxProd;
    }
}
相关推荐
码出财富8 小时前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
多米Domi0118 小时前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
我是小疯子668 小时前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
森叶8 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
二哈喇子!8 小时前
Eclipse中导入外部jar包
java·eclipse·jar
微露清风9 小时前
系统性学习C++-第二十二讲-C++11
java·c++·学习
Lips6119 小时前
2026.1.16力扣刷题
数据结构·算法·leetcode
进阶小白猿9 小时前
Java技术八股学习Day20
java·开发语言·学习
gis开发9 小时前
【无标题】
java·前端·javascript
Wpa.wk9 小时前
性能测试 - 搭建线上的性能测试环境参考逻辑图
java·经验分享·测试工具·jmeter·性能测试