题目

image.png

解题思路

image.png

作者:tong-zhu
链接:https://leetcode-cn.com/problems/maximum-product-of-word-lengths/solution/tong-ge-lai-shua-ti-la-zhao-dao-ti-mu-de-y37h/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

解题代码

  1. class Solution {
  2. public int maxProduct(String[] words) {
  3. int[] bits = new int[words.length];
  4. for(int i = 0; i< words.length; i++) {
  5. char[] chars = words[i].toCharArray();
  6. for(int j = 0; j < chars.length; j++) {
  7. bits[i] |= 1 << (chars[j] - 'a' );
  8. }
  9. }
  10. int max = 0;
  11. for(int i = 0; i < bits.length; i++ ) {
  12. for(int j = i + 1; j < bits.length; j++ ) {
  13. if( (bits[i] & bits[j] ) == 0 ) {
  14. max = Math.max(max, (words[i].length() * words[j].length()) );
  15. }
  16. }
  17. }
  18. return max;
  19. }
  20. }