diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /max_product_word_len.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'max_product_word_len.cpp')
-rw-r--r-- | max_product_word_len.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/max_product_word_len.cpp b/max_product_word_len.cpp new file mode 100644 index 0000000..c2e518d --- /dev/null +++ b/max_product_word_len.cpp @@ -0,0 +1,31 @@ +#include "leetcode.h" + +class Solution { +public: + int maxProduct(vector<string> &words) { + int n = words.size(), ans = 0; + vector<bitset<26>> chars(n); + for (int i = 0; i < n; i++) { + for (auto &ch : words[i]) + chars[i][ch - 'a'] = 1; + for (int j = 0; j < i; j++) + if (!helper(chars[i], chars[j])) + ans = max(ans, (int)words[i].size() * (int)words[j].size()); + } + return ans; + } + +private: + bool helper(bitset<26> &a, bitset<26> &b) { + for (int i = 0; i < 26; i++) + if (a[i] && b[i]) + return true; + return false; + } +}; + +int main() { + Solution obj; + vector<string> words = {"abcw", "baz", "foo", "bar", "xtfn", "abcdef"}; + cout << obj.maxProduct(words); +} |