-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaximum Product of Word Lengths.cpp
49 lines (36 loc) · 1.36 KB
/
Maximum Product of Word Lengths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Solution by Rahul Surana
***********************************************************
Given a string array words, return the maximum value of length(word[i]) * length(word[j])
where the two words do not share common letters. If no such two words exist, return 0.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int maxProduct(vector<string>& words) {
vector<vector<bool>> wvec(words.size(),vector<bool>(26,false));
vector<vector<bool>> wwr(words.size(),vector<bool>(words.size(),false));
for(int i = 0 ; i < words.size(); i++){
// p[i] = i;
for(int j = 0; j < words[i].size(); j++){
wvec[i][words[i][j] - 'a'] = true;
}
}
int ans = 0;
for(int i = 0; i < words.size(); i++){
for(int j = i+1; j < words.size(); j++){
for(int k = 0; k < 26; k++){
if(wvec[i][k] && wvec[j][k]){
wwr[i][j] = true;
break;
}
}
if(!wwr[i][j] && ans < words[i].size()*words[j].size()){
ans = words[i].size()*words[j].size();
}
}
}
return ans;
}
};