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 /naming_company.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'naming_company.cpp')
-rw-r--r-- | naming_company.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/naming_company.cpp b/naming_company.cpp new file mode 100644 index 0000000..24de89f --- /dev/null +++ b/naming_company.cpp @@ -0,0 +1,37 @@ +// 2306. Naming a Company +#include "leetcode.h" + +/* + * you are given an array of strings 'ideas' that represents a list of names to + * be used in the process o naming a company. the process of naming a company is + * as follows: chose 2 distinct names from 'ideas', call them 'ideaa, ideab'. + * swap the first letters of 'ideaa & ideab' with each other. + * if both the new names are not found in the original 'ideas', then the name + * 'ideaa ideab' (concatenated, separated by space) is a valid company name. + */ + +class Solution { +public: + long long distinctNames(vector<string> &ideas) { + long long ans = 0, cnt[26][26] = {}; + unordered_set<string> us[26]; + for (auto &i : ideas) + us[i[0] - 'a'].insert(i.substr(1)); + for (int i = 0; i < 26; ++i) + for (auto &suffix : us[i]) + for (int j = 0; j < 26; ++j) + cnt[i][j] += us[j].count(suffix) == 0; + for (int i = 0; i < 26; ++i) + for (int j = 0; j < 26; ++j) + ans += cnt[i][j] * cnt[j][i]; + return ans; + } +}; + +int main() { + Solution obj; + vector<string> ideas1 = {"coffee", "donuts", "time", "toffee"}, + ideas2 = {"lack", "back"}; + cout << obj.distinctNames(ideas1) << endl; // expect: 6 + cout << obj.distinctNames(ideas2) << endl; // expect: 0 +} |