题目
思路
检查两个字符串是否具有字符交集。如果没有,求字符串数组中两个字符串长度之积的最大值。
每个字符串只由小写字母组成,我们可以用分别用数组记录字符串各个字符是否出现,数组的大小即为26,对于同一个字符串,出现了重复字符无所谓,但是两个字符串之间不能出现字符交集即可。
class Solution {
public int maxProduct(String[] words) {
int maxLen = 0;
for(int i = 0; i < words.length; i ++){
for(int j = i + 1; j < words.length; j ++){
if(noCommonCharac(words[i],words[j])){
maxLen = Math.max(maxLen, words[i].length() * words[j].length());
}
}
}
return maxLen;
}
//The two strings whether have common characters
public boolean noCommonCharac(String a,String b){
boolean[] chaA = new boolean[26];
boolean[] chaB = new boolean[26];
//record whether each character of the String appears
for(int i = 0;i < a.length(); i++){
chaA[a.charAt(i) - 'a'] = true;
}
//record whether each character appears
for(int i = 0;i < b.length(); i++){
chaB[b.charAt(i) - 'a'] = true;
}
//check whether is a common character.
for(int i =0; i < 26; i++){
if(chaA[i] == chaB[i] && chaA[i] == true){
return false;
}
}
return true;
}
}
时间复杂度:
O
(
N
2
∗
(
L
1
+
L
2
)
)
O(N^2 * (L1 +L2))
O(N2∗(L1+L2)) ,其中L1、L2分别是两个字符串的长度。
空间复杂度:
O
(
1
)
O(1)
O(1)
预处理
对于上面的代码,可以进行继续的优化,上面的两层for循环每次调用noCommonCharac函数时都需要重复的比较字符串L1+L2。能不能记录每个字符串中各个字符的包含情况呢?
如果是上面的代码,可以用个二维数组,记录各个字符串的字符包含情况。先进行预处理,用二维数组来存放各个字符串的字符包含情况。
class Solution {
public int maxProduct(String[] words) {
int maxLen = 0;
boolean[][] charAs = new boolean[words.length][26];
for(int i = 0;i < words.length;i++){
for(int j = 0;j < words[i].length(); j++){
charAs[i][words[i].charAt(j) - 'a'] = true;
}
}
for(int i = 0; i < words.length; i ++){
for(int j = i + 1; j < words.length; j ++){
if(noCommonCharac(charAs[i],charAs[j])){
maxLen = Math.max(maxLen, words[i].length() * words[j].length());
}
}
}
return maxLen;
}
public boolean noCommonCharac(boolean[] a,boolean[] b){
for(int i =0; i < 26; i++){
if(a[i] == b[i] && a[i] == true){
return false;
}
}
return true;
}
}
时间复杂度
O
(
N
2
)
O(N^2)
O(N2):
空间复杂度
O
(
N
)
O(N)
O(N):需要存储各个字符串的字符包含情况,二维数组。
位运算
前面第一次的代码是,用Boolean数组来存放每个字符的出现情况,但是其实可以不用,用位运算也能存放,整数不是有32位二进制吗?拿出前26位,每一位用来存放字符串字符包含情况,这样一个整数就能存放字符串每个字符的出现情况了。
比如bceg字符串,通过(int)word[i] - 'a’得到位置n,然后1 << n,也能设置相应的二进制位,整数可以通过或运算得到整个字符串的各个字符的出现的情况。
10 | 100 | 1000 | 1000000 = 1010110
如果两个字符串没有字符的交集,那么通过位运算的结果,相与之后的二进制是没有1的。
class Solution {
public int maxProduct(String[] words) {
int maxLen = 0;
for(int i = 0; i < words.length; i ++){
for(int j = i + 1; j < words.length; j ++){
if(noCommonCharac(words[i],words[j])){
maxLen = Math.max(maxLen, words[i].length() * words[j].length());
}
}
}
return maxLen;
}
public boolean noCommonCharac(String worda,String wordb){
int a = 0;
int b = 0;
for(int i = 0; i < worda.length(); i++){
a |= 1 << (worda.charAt(i) - 'a');
}
for(int i = 0;i < wordb.length(); i++){
b |= 1 << (wordb.charAt(i) - 'a');
}
return (a & b) == 0;
}
}
时间复杂度:
O
(
N
2
∗
(
L
1
+
L
2
)
)
O(N^2 * (L1 +L2))
O(N2∗(L1+L2)) ,其中L1、L2分别是两个字符串的长度。
空间复杂度:
O
(
1
)
O(1)
O(1)
位运算+预处理
综合了上面代码各个优点,使用预处理,将每个字符串各个字符的包含出现情况,用一个整型数组appears 存起来,数组的每个元素就是字符串的各个字符的出现情况。同时也把字符串的长度用一个整型数组lens存起来。
class Solution {
public int maxProduct(String[] words) {
int maxLen = 0;
int[] appears = new int[words.length];
int[] lens = new int[words.length];
for(int i = 0;i < words.length;i++){
int a = 0;
for(int j = 0;j < words[i].length(); j++){
a |= 1 << (words[i].charAt(j) - 'a');
}
appears[i] = a;
lens[i] = words[i].length();
}
for(int i = 0; i < words.length; i ++){
for(int j = i + 1; j < words.length; j ++){
if((appears[i] & appears[j]) == 0){
maxLen = Math.max(maxLen, lens[i] * lens[j]);
}
}
}
return maxLen;
}
}
时间复杂度:
O
(
N
2
)
O(N^2)
O(N2)
空间复杂度:
O
(
N
)
O(N)
O(N)