#2306

Naming a Company

candidate master · 1520 · lc hard +32 · verified · 46.5% accepted · 1,966 likes · top 31%

Description

You are given an array of strings ideas as candidates for a company name. A company name is formed by:

- Picking two distinct strings ideaA and ideaB from ideas.

- Swapping their first characters.

- If neither of the resulting strings appears in ideas, the pair ideaA ideaB is a valid company name.

Return the total number of distinct valid company names.

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.

Example 2:

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 3:

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

Code

1
2
3