给你一个字符串数组 words
,找出并返回 length(words[i]) * length(words[j])
的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0
。
示例 1:
输入:words = ["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16
解释
:这两个单词为 "abcw", "xtfn"
。
class Solution {
public int maxProduct(String[] words) {
Arrays.sort(words, (o1, o2) -> {
int a = o2.length(), b = o1.length();
return a - b;
});
int max = 0;
for (int i = 0; i < words.length - 1; i++) {
aa:
for (int j = i + 1; j < words.length; j++) {
if (words[i].length() * words[j].length() < max) break;
for (int k = 0; k < words[j].length(); k++) {
char a = words[j].charAt(k);
if (words[i].indexOf(a) >= 0) continue aa;
}
max = words[i].length() * words[j].length();
break;
}
}
return max;
}
}