I used brute force to find out that I couldn't pass, and that one of the test cases used a single string that was very long
My code is as follows
public class Solution {public
int maxproduct (string[] words) {
int max = 0;
Arrays.sort (words, new comparator<string> () {public
int compare (string A, string b) {return
b.length ()-A . Length ();
}
});
for (int i = 0;i < words.length;i++) {for
(int j = i + 1;j < words.length;j++) {
if!checkshare (words[i],words[ J]) {
max = Math.max (Max,words[i].length () * words[j].length ());
}}} return max;
}
Private Boolean checkshare (String a,string b) {for
(int i = 0;i < A.length (); i++) {
if B.indexof ("" +a.charat (i))!=-1) return
true;
}
return false;
}
}
Found that top solution are fully sufficient to save 26 characters with a total of 32 bits that are not in operation.
public static int maxproduct (string[] words) {
if (words = = NULL | | words.length = 0) return
0;
int len = words.length;
int[] value = new Int[len];
for (int i = 0; i < len; i++) {
String tmp = words[i];
Value[i] = 0;
for (int j = 0; J < Tmp.length (); j + +) {
value[i] |= 1 << (Tmp.charat (j)-' a ');
}
int maxproduct = 0;
for (int i = 0; i < len; i++) for
(int j = i + 1; j < Len; J +) {
if (Value[i] & value[j]) = 0 & & (Words[i].length () * Words[j].length () > Maxproduct))
maxproduct = Words[i].length () * words[j].length ();
}
return maxproduct;
}
In the next admire
2017/1/6