Watch 10 video solutions for Naming a Company, a hard level problem involving Array, Hash Table, String. This walkthrough by NeetCodeIO has 12,674 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array of strings ideas that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:
ideas, call them ideaA and ideaB.ideaA and ideaB with each other.ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.Return the number of distinct valid names for the company.
Example 1:
Input: ideas = ["coffee","donuts","time","toffee"]
Output: 6
Explanation: The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
Example 2:
Input: ideas = ["lack","back"] Output: 0 Explanation: There are no valid selections. Therefore, 0 is returned.
Constraints:
2 <= ideas.length <= 5 * 1041 <= ideas[i].length <= 10ideas[i] consists of lowercase English letters.ideas are unique.Problem Overview: You receive a list of idea names. Pick two ideas, swap their first letters, and form two new names. A pair is valid only if both newly formed names do not already exist in the original list. The task is to count how many distinct valid company name pairs can be created.
Approach 1: Brute Force Pair Enumeration (O(n² * L) time, O(n) space)
Check every pair of ideas using two nested loops. For each pair, swap the first characters and build the two new candidate strings. Use a HashSet to quickly verify whether either generated name already exists. If both are new, the pair contributes two valid company names (since order matters). This approach directly simulates the operation and is useful for understanding the constraints, but the quadratic pair checks make it impractical for large inputs.
Approach 2: Optimized Using Prefix Sets (O(n + 26² * k) time, O(n) space)
Instead of comparing every pair of full strings, group ideas by their first character and store only the suffix (everything after the first letter) in a hash set. This creates up to 26 groups. When comparing two groups i and j, compute how many suffixes they share. Shared suffixes would produce existing names after swapping prefixes, so they must be excluded. If group sizes are a and b and they share common suffixes, the number of valid combinations is (a - common) * (b - common) * 2. This avoids constructing every possible pair and reduces the search space to prefix group comparisons.
The optimization relies heavily on fast lookups provided by a hash table and grouping strings by their first character. Handling suffix sets efficiently keeps comparisons small even when the input size grows. The logic mainly involves string slicing and enumerating the 26 prefix groups stored in an array or list.
Recommended for interviews: Interviewers expect the prefix-group optimization. The brute force approach demonstrates the core idea of swapping characters and validating with a set, but the optimized solution shows you can reduce redundant comparisons by grouping data and counting valid combinations mathematically.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Enumeration | O(n² * L) | O(n) | Useful for understanding the swapping logic or when the number of ideas is very small. |
| Prefix Grouping with Suffix Hash Sets | O(n + 26² * k) | O(n) | Best general solution. Efficient for large inputs by avoiding pairwise string comparisons. |