LeetCode 318. Maximum Product of Word Length 解题报告
2016-10-15 18:14
411 查看
LeetCode 413. Arithmetic Slices 解题报告
题目描述
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.示例
Example 1:Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0
No such pair of words.
限制条件
没有明确给出.解题思路
我的思路:
分成两步来解决:1.处理所有字符串,记录它们包含的所有字母信息
2.两两比较所有的字符串的字母信息,如果均不相同并且长度积比当前的长度积要大,就更新长度积。最后返回长度积。
实现的时候就是用一个vector保存字符串的字母信息,比较字符串就是用之前解题时的方式,设一个大小为26的数组,扫描两个字符信息去更新这个数组,根据扫描结果得到比较结果,最后就是两个循环,进行两两比较并更新长度积。
然而,通过是通过了,但是时间却是很感人,仅比3%的通过代码快,几乎垫底了。。。所以还是看看大牛们的解法吧。
参考思路
核心思路就是我上面的两步,然而实现上却是有很多绝妙的技巧。1.字符的字母信息用一个整型数表示,整型数的0-25bit对应着a-z,如果存在a,那么该整型数的第0bit就是1,这是通过位操作运算实现:对于1移动字符c - a个位置,然后与当前字母信息进行或运算。
2.设置一个unordered_map保存字符串的字母信息及长度,因此当存[aa],[aaaa]这类字母信息相同但是长度不同的字符串时,只有最长的长度会保存在unordered_map中,从而减少了比较的次数。
3.比较是在unordered_map中进行,而不是原始的字符串数组,通过相与两个字符串对应的字母信息,结果为1表明存在相同字母,为0则是没有相同字母,在为0时更新长度积。
最后返回长度积即可。
代码
我的代码
class Solution { public: int maxProduct(vector<string>& words) { int product = 0; vector<string> unique(words.size()); string letters = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < words.size(); i++) { for (int j = 0; j < 26; j++) { if (words[i].find_first_of(letters[j]) != string::npos) unique[i] += letters[j]; } } for (int i = words.size() - 1; i > 0; i--) for (int j = i - 1; j >= 0; j--) if (!hasCommon(unique[i], unique[j])) product = max(product, (int)(words[i].size() * words[j].size())); return product; } bool hasCommon(string s1, string s2) { int count[26] = {0}; for (auto c: s1) count[c - 'a']++; for (auto c: s2) { if (count[c- 'a']) return true; } return false; } };
参考代码
class Solution { public: int maxProduct(vector<string>& words) { unordered_map<int, int> maxlen; int result = 0; for (string word: words) { int mask = 0; for (char c: word) mask |= 1 << (c - 'a'); maxlen[mask] = max(maxlen[mask], (int) word.size()); } for (auto a: maxlen) { for (auto b: maxlen) { if (!(a.first & b.first)) result = max(result, a.second * b.second); } } return result; } };
总结
这道题不难,然而要实现出高效的解法却不容易,我的解法是最最平庸的,所以不可取,从大牛们的解法中,我又学到了字符串的字母信息不仅可以用一个大小为26的数组表示,更可以直接通过位运算用整型数表示,真是好厉害!!!!然后unorder_map的使用也是很巧妙,佩服得不行,从这道题学习到了很多东西,超级开心点的,(^__^) 嘻嘻……。又填好了一个坑,精彩明天继续!加油加油!
相关文章推荐
- Leetcode 127. Word Ladder 字符变换 解题报告
- [Leetcode] 126. Word Ladder II 解题报告
- [leetcode] 127. Word Ladder 解题报告
- leetcode解题之58 # Length of Last Word Java版
- [Leetcode] 127. Word Ladder 解题报告
- [leetcode]126. Word LadderII@Java解题报告
- [leetcode] 140. Word Break II 解题报告
- [Leetcode] Length of Last Word 解题报告
- [Leetcode] 244. Shortest Word Distance II 解题报告
- [Leetcode] 524. Longest Word in Dictionary through Deleting 解题报告
- 【LeetCode】Word Break II 解题报告
- 【LeetCode】291.Word Pattern II(Hard)解题报告
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
- LeetCode: Word Ladder II 解题报告
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
- 【LeetCode】244.Shortest Word Distance II(Medium)解题报告
- 【LeetCode】Word Break 解题报告
- [LeetCode]Add and Search Word - Data structure design,解题报告
- [Leetcode] 245. Shortest Word Distance III 解题报告
- leetcode 解题报告 Word Ladder II