很巧妙啊,,用素数乘积能否被整除来表征是否可以将字符全部包含
class Solution {
private static final int[] prime = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 , 103 };
public String shortestCompletingWord(String licensePlate, String[] words) {
long product = getStringProduct( licensePlate.toLowerCase() );
String shortest = "aaaaaaaaaaaaaaaaaaaa";
for( String word : words ){
long ll = getStringProduct( word );
if( word.length() < shortest.length() && ll % product == 0 )
shortest = word;
}
return shortest;
}
private long getStringProduct( String ss ){
long res = 1L;
for( char c : ss.toCharArray() ){
int index = c - 'a';
if( index >= 0 && index <= 25 )
res *= prime[ index ];
}
return res;
}
}