当前位置:网站首页>leetcode-318.最大单词长度乘积
leetcode-318.最大单词长度乘积
2022-08-02 05:15:00 【KGundam】
位运算
题目详情
给你一个字符串数组 words ,找出并返回 length(words[i]) * length(words[j]) 的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0 。
示例1:
输入:words = ["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16
解释:这两个单词为 "abcw", "xtfn"。
示例2:
输入:words = ["a","ab","abc","d","cd","bcd","abcd"]
输出:4
解释:这两个单词为 "ab", "cd"。
示例3:
输入:words = ["a","aa","aaa","aaaa"]
输出:0
解释:不存在这样的两个单词。
思路:
本题的关键显然是判断两个单词是否有相同字母,如果遍历两两比较,显然复杂度较高
我们可以利用位运算,因为题目要求两个无相同字母的最长的长度乘积,所以我们可以建立一个hash
利用位左移运算得到每个字母独特的1的位置(int类型长度为32,字母一共26个,所以位置足够用)
再利用或运算|=将其赋予mask,最后每个单词都会有自己的一个独特的mask
也有可能存在一模一样mask的单词(存在重复字母比如meet和met的mask就相同)
我们只需要每次更新mask对应的length为较长的即可
在我们遍历一个个单词的时候,遍历完一个我们就用这个mask和hash里已存的mask按位与
当按位与为0时就更新ans为较长的乘积,最后我们遍历完单词,也求出了最终答案
我的代码:
class Solution
{
public:
int maxProduct(vector<string>& words)
{
unordered_map<int, int> hash; //建立hash存储字符串
int ans = 0;
for (const string & word : words) //对于每个单词
{
int mask = 0, size = word.size();
for (const char & c : word) //每个单词的每个字母
{
//c-'a'就是c到a的距离,然后将1左移这么多距离
//就能将000..01这个1移到专属于c的一个位置
//然后利用mask与它做或运算,从而将这个位置的1
//赋给mask,最后遍历完单词每个字母,就会得到一个独特的总数
mask |= 1 << (c - 'a');
}
//如果有同mask(有重复字母)的词我们取较长的
//例如meet和met具有相同的mask,我们取length就是4
hash[mask] = max(hash[mask], size);
//遍历hash寻找满足的h_mask
for (const auto& [h_mask, h_len]: hash)
{
if (!(mask & h_mask)) //按位与如果不存在相同字母
{
ans = max(ans, size * h_len);//就更新ans为较大值
}
}
}
return ans;
}
};
位运算常用技巧

边栏推荐
- 【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
- 51 MCU peripherals: DS18B20
- How H5 realizes evoking APP
- Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记
- Constructors, member variables, local variables
- The Go language learning notes - dealing with timeout - use the language from scratch from Context
- 腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
- Meta公司内部项目-RaptorX:将Presto性能提升10倍
猜你喜欢

线程基础(一)

Three methods of importing sql files in MySQL

国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐

Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer

非关系型数据库MongoDB的特点及安装

Stress testing and performance analysis of node projects

为什么4个字节的float要比8个字节的long大呢?

BGP实验(路由反射器,联邦,路由优化)

Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战

oracle 远程连接数据库
随机推荐
Brush LeetCode topic series - 10. Regular expression match
Stress testing and performance analysis of node projects
nacos注册中心
About the directory structure of the web application
Install and use Google Chrome
路由规划中级篇
目标检测重要概念——IOU、感受野、空洞卷积、mAP
Alluxio为Presto赋能跨云的自助服务能力
配合蓝牙打印的encoding-indexes.js文件内容:
Differences between i++ and ++i in loops in C language
分布式文件存储服务器之Minio对象存储技术参考指南
反向解析dns服务器
[PSQL] window function, GROUPING operator
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
C语言小游戏——扫雷小游戏
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
Automated operation and maintenance tools - ansible, overview, installation, module introduction
eggjs controller层调用controller层解决方案
Home NAS server (4) | MergerFS and SnapRaid data backup
The advantages of making web3d dynamic product display