LeetCode Problem

How to Solve Naming a Company

This problem asks you to count valid company name pairs by swapping prefixes and suffixes. The solution involves scanning through the ideas array and using a hash table to track valid combinations. By considering the array elements carefully and leveraging efficient data structures, the problem can be solved efficiently even for larger inputs.

GhostInterview Help

Need help with Naming a Company without spending extra time grinding it?

GhostInterview can read Naming a Company from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2306Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem asks you to count valid company name pairs by swapping prefixes and suffixes. The solution involves scanning through the ideas array and using a hash table to track valid combinations. By considering the array elements carefully and leveraging efficient data structures, the problem can be solved efficiently even for larger inputs.

Problem Statement

You are given an array of strings called ideas representing possible names for a company. The goal is to find the number of valid distinct company names that can be formed by swapping the first letter of two names from this array. After swapping the first letters, if the resulting name combination already exists in the array, it is considered invalid.

Return the number of distinct valid company names that can be formed. For example, with ideas = ["coffee","donuts","time","toffee"], valid pairs are created by swapping the first letters of different names without forming an existing name in the array.

Examples

Example 1

Input: ideas = ["coffee","donuts","time","toffee"]

Output: 6

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

There are no valid selections. Therefore, 0 is returned.

Constraints

  • 2 <= ideas.length <= 5 * 104
  • 1 <= ideas[i].length <= 10
  • ideas[i] consists of lowercase English letters.
  • All the strings in ideas are unique.

Solution Approach

Array Scanning and Hash Table Usage

The problem requires scanning through the array of name ideas while simultaneously tracking prefixes in a hash table. For each name, the first letter is replaced with the first letter of another name, and a hash set is used to check if this combination already exists. Efficiently storing combinations in a hash table allows quick lookups to avoid invalid name formations.

Efficient Pair Enumeration

For every possible pair of names, you swap the first letter and check if the resulting combination exists in the hash table. If not, count it as valid. By iterating over all the pairs and using the hash table for fast validation, the solution achieves good performance.

Consideration of Edge Cases

Ensure that invalid pairs are accounted for, such as when the swapped names are identical or when the combination already exists in the original list. These edge cases should be filtered out before counting valid names.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of this problem depends on the approach used to scan through the array and look up name combinations. With array scanning and hash table usage, the solution should be efficient, likely running in O(n^2) time where n is the number of names. Space complexity is determined by the size of the hash table, which depends on the number of unique prefix combinations.

What Interviewers Usually Probe

  • The candidate demonstrates understanding of hash table operations and can apply them to solve array-related problems efficiently.
  • The candidate identifies key edge cases, such as ensuring valid name combinations and avoiding duplicates.
  • The candidate suggests optimizations based on the size of the input and efficiently handles larger datasets.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to filter out invalid pairs that are formed by identical swapped names.
  • Not handling edge cases where the name formed by swapping already exists in the original list.
  • Overcomplicating the solution or using nested loops inefficiently instead of hash lookups.

Follow-up variants

  • Consider solving this problem by optimizing the space complexity with a more compact representation of name prefixes.
  • Explore an approach that minimizes redundant checks when evaluating pairs with common prefix letters.
  • Modify the problem to allow case sensitivity or additional string modifications, such as swapping middle characters.

How GhostInterview Helps

  • GhostInterview helps by providing step-by-step guidance for solving this problem with the right data structures, such as hash tables.
  • With personalized hints and examples, GhostInterview ensures you understand the failure modes and how to avoid them efficiently.
  • GhostInterview assists you in practicing efficient enumeration and hash lookups, ensuring you can handle larger datasets.

Topic Pages

FAQ

What is the optimal approach for solving the "Naming a Company" problem?

The optimal approach involves scanning through the list of names and using a hash table to track valid name combinations formed by swapping prefixes. This ensures efficient validation.

What is the primary pattern in the "Naming a Company" problem?

The primary pattern involves array scanning combined with hash lookup to validate name combinations formed by swapping prefixes.

How can I optimize the solution for larger input sizes?

For larger inputs, optimize the hash table usage to minimize redundant checks and carefully manage space complexity to handle more combinations efficiently.

How do I handle duplicate names when forming new company names?

Ensure to filter out combinations that result in the same names as those in the original list, which would be considered invalid.

What edge cases should I consider in this problem?

Key edge cases include identical names after swapping prefixes and ensuring no invalid names are counted when formed by already existing names.

GhostInterview Solver

Need direct help with Naming a Company instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Naming a Company from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.