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;
    }
}
相关推荐
夏鹏今天学习了吗5 小时前
【LeetCode热题100(66/100)】寻找两个正序数组的中位数
算法·leetcode·职场和发展
墨染点香5 小时前
LeetCode 刷题【151. 反转字符串中的单词】
算法·leetcode·职场和发展
跟着珅聪学java5 小时前
HttpServletRequest中的 Attribute(属性)生命周期和作用域是 Java Web 开发中的重要概念
java
m0_495562785 小时前
Swift-static和class
java·服务器·swift
合作小小程序员小小店6 小时前
web网页开发,在线物流管理系统,基于Idea,html,css,jQuery,jsp,java,SSM,mysql
java·前端·后端·spring·intellij-idea·web
这周也會开心7 小时前
SpringMVC整理
java·springmvc
東雪木7 小时前
Spring Boot 2.x 集成 Knife4j (OpenAPI 3) 完整操作指南
java·spring boot·后端·swagger·knife4j·java异常处理
数学难7 小时前
Java面试题2:Java线程池原理
java·开发语言
Charles_go7 小时前
C#8、有哪些访问修饰符
java·前端·c#
qwer12321ck767 小时前
srcType instanceof Class 及泛型 vs 普通类
java