当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 5: Maximum product of word length
subject 1:
Given an array of strings words, Please calculate when two strings words[i] and words[j] Does not contain the same characters , The maximum of the product of their lengths . Suppose that the string contains only English lowercase letters . If there is no pair of strings that do not contain the same characters , return 0.
Example 1:
Input : words = [“abcw”,“baz”,“foo”,“bar”,“fxyz”,“abcdef”]
Output : 16
explain : These two words are “abcw”, “fxyz”. They do not contain the same characters , And the product of length is the largest .
Example 2:
Input : words = [“a”,“ab”,“abc”,“d”,“cd”,“bcd”,“abcd”]
Output : 4
explain : These two words are “ab”, “cd”.
Example 3:
Input : words = [“a”,“aa”,“aaa”,“aaaa”]
Output : 0
explain : There are no such two words .
Tips :
2 <= words.length <= 1000
1 <= words[i].length <= 1000
words[i] Only lowercase letters
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/aseY1I
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
The number of letters is only 26 individual , We can use one int 32 To record whether the word contains the letter
such as
“ac”0101
ac: Last 1 On behalf of a There is , Next to last 0 representative b non-existent
“bd”1010
ac Binary system & bd If the binary of is 0, Prove different , On the contrary, they are the same
- take word Convert to binary time complexity O(n*k)
- Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity O(n2)
Code
func maxProduct(words []string) int {
// Ideas :
// * take word Convert to binary time complexity O(n*k)
// * Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity O(n2)
// Processing parameters
if len(words) < 2 {
return 0
}
// take word Convert to binary time complexity O(n*k)
nums := make([]int,len(words))
for i:=0;i<len(words);i++ {
for j:=0;j<len(words[i]);j++{
nums[i] |= (1<<(words[i][j] - 'a'))
}
}
// Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity O(n2)
max := 0
for i:=0;i<len(words);i++ {
for j:=i + 1;j<len(words);j++{
if nums[i] & nums[j] == 0 {
tempMax := len(words[i]) * len(words[j])
if tempMax > max {
max = tempMax
}
}
}
}
return max
}
test
边栏推荐
- Teach you to release a DeNO module hand in hand
- Single chip Bluetooth wireless burning
- 【无标题】
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- 堆排序【手写小根堆】
- FairyGUI按钮动效的混用
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- Matlab读取GNSS 观测值o文件代码示例
- [算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
- Easy to use shortcut keys in idea
猜你喜欢
Idea problem record
In 2020, the average salary of IT industry exceeded 170000, ranking first
341. Flatten nested list iterator
Mixed use of fairygui button dynamics
平衡二叉树详解 通俗易懂
【干货】提升RTK模糊度固定率的建议之周跳探测
Fairygui character status Popup
rtklib单点定位spp使用抗差估计遇到的问题及解决
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
随机推荐
使用rtknavi进行RT-PPP测试
FairyGUI簡單背包的制作
Containers and Devops: container based Devops delivery pipeline
Fairygui gain buff value change display
FairyGUI条子家族(滚动条,滑动条,进度条)
【无标题】
Idea problem record
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
平衡二叉树详解 通俗易懂
Expected value (EV)
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
Introduction to the daily practice column of the Blue Bridge Cup
FGUI工程打包发布&导入Unity&将UI显示出来的方式
程序设计大作业:教务管理系统(C语言)
KF UD分解之伪代码实现进阶篇【2】
Fabrication d'un sac à dos simple fairygui
[leetcode19] delete the penultimate node in the linked list
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
[算法] 剑指offer2 golang 面试题10:和为k的子数组