Class Solution
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:
- Choose 2 distinct names from
ideas
, call themideaA
andideaB
. - Swap the first letters of
ideaA
andideaB
with each other. - If both of the new names are not found in the original
ideas
, then the nameideaA ideaB
(the concatenation ofideaA
andideaB
, separated by a space) is a valid company name. - 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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
distinctNames
-