给定一个字符串数组 words,请计算当两个字符串 words[i] 和 words[j] 不包含相同字符时,它们长度的乘积的最大值。假设字符串中只包含英语的小写字母。如果没有不包含相同字符的一对字符串,返回 0。
for(int i=0;i<words.length;i++){
for(int j=0;j<words.length;j++){
1 2 3 4
1 2 3 4
第一轮:1跟 2 3 4比较,第二轮:2就不需要跟1 比较了,只需要跟3 4比即可。
class Solution {
public int maxProduct(String[] words) {
int maxLength=0;
for(int i=0;i<words.length;i++){
for(int j=0;j<words.length;j++){
int flag=0;
for(int k=0;k<words[i].length();k++){
for(int l=0;l<words[j].length();l++){
if(words[i].charAt(k)==words[j].charAt(l)){
flag=1;//证明有相同字符
break;
}
}
if(flag==1) break;
}
if(flag==0) {
maxLength=
maxLength>words[i].length()*words[j].length() ?
maxLength : words[i].length()*words[j].length();
}
}
}
return maxLength;
}
}
时间复杂度:0(N^4) 明显超时了
class Solution {
public int maxProduct(String[] words) {
int maxLength=0;
for(int i=0;i<words.length;i++){
for(int j=i+1;j<words.length;j++){
if(judgeSameString(words[i],words[j])==false){
maxLength=maxLength>words[i].length()*words[j].length() ? maxLength :words[i].length()*words[j].length();
}
}
}
return maxLength;
}
private boolean judgeSameString(String str1,String str2){
boolean flag=false;
for(char a : str1.toCharArray()){
if(str2.indexOf(a)!=-1){
flag=true; //不等于-1证明 两个字符串有相同字符,则不用比较
break;
}
}
return flag;
}
}
时间复杂度:O(N^2* m^2)
class Solution {
public int maxProduct(String[] words) {
int maxLength=0;
for(int i=0;i<words.length;i++){
for(int j=i+1;j<words.length;j++){
if(judgeSameString(words[i],words[j])==false){
maxLength=maxLength>words[i].length()*words[j].length() ? maxLength :words[i].length()*words[j].length();
}
}
}
return maxLength;
}
private boolean judgeSameString(String str1,String str2){
boolean flag=false;
int array[]=new int[26];
for(char c: str1.toCharArray()) array[c-'a']=1;
for(char c: str2.toCharArray()){
if(array[c-'a']!=0){
flag=true;
break;
}
}
return flag;
}
}
时间复杂度:O(N^2*M)
class Solution {
public int maxProduct(String[] words) {
int maxLength=0;
for(int i=0;i<words.length;i++){
for(int j=i+1;j<words.length;j++){
if(judgeSameString(words[i],words[j])==false){
maxLength=maxLength>words[i].length()*words[j].length() ? maxLength :words[i].length()*words[j].length();
}
}
}
return maxLength;
}
private boolean judgeSameString(String str1,String str2){
int bitMask1 = 0, bitMask2 = 0;
for (char c : str1.toCharArray()) bitMask1 |= (1 << (c - 'a'));
for (char c : str2.toCharArray()) bitMask2 |= (1 << (c - 'a'));
return (bitMask1 & bitMask2) != 0;
}
}
// 位运算 + 预计算
// 时间复杂度:O((m + n)* n)
// 空间复杂度:O(n)
public int maxProduct2(String[] words) {
// O(mn)
int n = words.length;
int[] masks = new int[n];
for (int i = 0; i < n; i++) {
int bitMask = 0;
for (char c : words[i].toCharArray()) {
bitMask |= (1 << (c - 'a'));
}
masks[i] = bitMask;
}
// O(n^2)
int ans = 0;
for (int i = 0; i < words.length; i++) {
String word1 = words[i];
for (int j = i + 1; j < words.length; j++) {
String word2 = words[j];
if ((masks[i] & masks[j]) == 0) {
ans = Math.max(ans, word1.length() * word2.length());
}
}
}
return ans;
}
作者:tangweiqun
链接:https://leetcode.cn/problems/aseY1I/solution/jian-dan-yi-dong-javac-pythonjs-zui-da-d-ffga/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
// 位运算 + 预计算
// 时间复杂度:O((m + n)* n)
// 空间复杂度:O(n)
public int maxProduct(String[] words) {
// O(mn)
Map<Integer, Integer> map = new HashMap<>();
int n = words.length;
for (int i = 0; i < n; i++) {
int bitMask = 0;
for (char c : words[i].toCharArray()) {
bitMask |= (1 << (c - 'a'));
}
// there could be different words with the same bitmask
// ex. ab and aabb
map.put(bitMask, Math.max(map.getOrDefault(bitMask, 0), words[i].length()));
}
// O(n^2)
int ans = 0;
for (int x : map.keySet()) {
for (int y : map.keySet()) {
if ((x & y) == 0) {
ans = Math.max(ans, map.get(x) * map.get(y));
}
}
}
return ans;
}
作者:tangweiqun
链接:https://leetcode.cn/problems/aseY1I/solution/jian-dan-yi-dong-javac-pythonjs-zui-da-d-ffga/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。