当前位置:网站首页>[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

边栏推荐
- Force buckle 1189 Maximum number of "balloons"
- (1) Introduction Guide to R language - the first step of data analysis
- idea问题记录
- Get the position of the nth occurrence of the string
- How to add music playback function to Arduino project
- 【干货】提升RTK模糊度固定率的建议之周跳探测
- KF UD分解之伪代码实现进阶篇【2】
- Prove the time complexity of heap sorting
- Agile development helps me
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
猜你喜欢

Office提示您的许可证不是正版弹框解决

Lock wait timeout exceeded try restarting transaction

Unity场景跳转及退出

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)

2021.11.10汇编考试

FairyGUI简单背包的制作

dosbox第一次使用

第一人称视角的角色移动
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
随机推荐
Easy to use shortcut keys in idea
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
MySQL error warning: a long semaphore wait
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
單片機藍牙無線燒錄
341. Flatten nested list iterator
2022国赛Re1 baby_tree
idea问题记录
[Leetcode15]三数之和
Minio file download problem - inputstream:closed
[Offer18]删除链表的节点
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
How to improve the deletion speed of sequential class containers?
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
2021.11.10汇编考试
Latex learning
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
FairyGUI增益BUFF數值改變的顯示
[899]有序队列