> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Naming a Company Python Solution with Tests

> Tested Python solution for LeetCode 2306 with 27 pytest cases. Generate a practice environment with lcpy.

LeetCode 2306, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/naming-a-company/description/).

Generate this problem as a practice environment: tested reference solution, 27 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2306   # by problem number
lcpy gen -s naming_a_company   # by problem name
```

## Problem

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:

1. Choose 2 **distinct** names from `ideas`, call them `ideaA` and `ideaB`.
2. Swap the first letters of `ideaA` and `ideaB` with each other.
3. If **both** of the new names are not found in the original `ideas`, then the name `ideaA ideaB` (the **concatenation** of `ideaA` and `ideaB`, separated by a space) is a valid company name.
4. Otherwise, it is not a valid name.

Return the number of **distinct** valid names for the company.

### Examples

```
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.
```

```
Input: ideas = ["lack","back"]
Output: 0
Explanation: There are no valid selections. Therefore, 0 is returned.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/naming_a_company/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/naming_a_company/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * L + 26^2 * S) where S is the largest group size
    # Space: O(n * L)
    def distinct_names(self, ideas: list[str]) -> int:
        groups: list[set[str]] = [set() for _ in range(26)]
        for idea in ideas:
            groups[ord(idea[0]) - ord("a")].add(idea[1:])

        total = 0
        for a in range(26):
            for b in range(a + 1, 26):
                common = len(groups[a] & groups[b])
                total += 2 * (len(groups[a]) - common) * (len(groups[b]) - common)
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n \* L + 26^2 \* S) where S is the largest group size | O(n \* L) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.