Sponsored
Sponsored
In this approach, we will iterate over all possible pairs of ideas and attempt to swap their first letters to form new names. We will then check if these new names are unique by not being present in the original list of ideas. This approach straightforwardly uses nested loops to examine each pair, which results in an O(n^2) time complexity for the pair-wise name generation.
Time Complexity: O(n^2 * m), where n is the number of ideas, and m is the average length of the ideas, due to the nested loop and string comparison.
Space Complexity: O(1) as we are not using any additional space proportional to input size beyond function parameters.
1function countValidNames(ideas) {
2 const ideaSet = new Set(ideas);
3 let validNames = 0;
4
5 for (let i = 0; i < ideas.length; i++) {
6 for (let j = i + 1; j < ideas.length; j++) {
7 const a = ideas[i];
8 const b = ideas[j];
9
10 const swappedA = b[0] + a.substring(1);
11 const swappedB = a[0] + b.substring(1);
12
13 if (!ideaSet.has(swappedA) && !ideaSet.has(swappedB)) {
14 validNames++;
15 }
16 }
17 }
18
19 return validNames * 2; // Counting both (a,b) and (b,a)
20}
21
22const ideas = ["coffee", "donuts", "time", "toffee"];
23console.log(countValidNames(ideas));
This JavaScript solution employs a Set for quick uniqueness verification. It iterates over possible idea pairs, swaps their first letters, and checks if those swapped ideas are present in the original set.
This improved approach seeks to avoid redundant swaps by organizing names into groups based on their prefix letters. If two sets have no overlap with original names when swapping the first characters of the names from those sets, we count the combinations. By limiting unnecessary swaps, this method reduces repetitive calculations.
Time Complexity: O(n^2 * m) but more efficient than brute force due to reduced checking within grouped sets.
Space Complexity: O(n) since we store ideas organized by prefix.
#include <vector>
#include <string>
#include <unordered_set>
int countValidNamesOptimized(const std::vector<std::string> &ideas) {
std::vector<std::unordered_set<std::string>> prefixSets(26);
int validNames = 0;
// Group ideas by their first character in prefixSets
for (const std::string &idea : ideas) {
char firstChar = idea[0];
prefixSets[firstChar - 'a'].insert(idea);
}
// Compare each set of prefixed names
for (int i = 0; i < 26; i++) {
for (int j = i + 1; j < 26; j++) {
for (const std::string &a : prefixSets[i]) {
for (const std::string &b : prefixSets[j]) {
std::string swappedA = b;
std::string swappedB = a;
swappedA[0] = a[0];
swappedB[0] = b[0];
if (prefixSets[i].find(swappedA) == prefixSets[i].end() && prefixSets[j].find(swappedB) == prefixSets[j].end()) {
validNames++;
}
}
}
}
}
return validNames * 2; // Count both (a,b) and (b,a)
}
int main() {
std::vector<std::string> ideas = {"coffee", "donuts", "time", "toffee"};
std::cout << countValidNamesOptimized(ideas) << std::endl;
return 0;
}
C++ version employs unordered sets segmented by initial letters for better performance in comparison tasks. This reduces computations, leading to enhanced efficiency when validating new names.