Class Solution

java.lang.Object
g2301_2400.s2306_naming_a_company.Solution

public class Solution extends Object
2306 - Naming a Company.

Hard

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.

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 * 104
  • 1 <= ideas[i].length <= 10
  • ideas[i] consists of lowercase English letters.
  • All the strings in ideas are unique.
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • distinctNames

      public long distinctNames(String[] ideas)