Class Solution

java.lang.Object
g2301_2400.s2306_naming_a_company.Solution

public class Solution extends Object
2306 - Naming a Company.<p>Hard</p> <p>You are given an array of strings <code>ideas</code> 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:</p> <ol> <li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li> <li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li> <li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li> <li>Otherwise, it is not a valid name.</li> </ol> <p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> ideas = [&ldquo;coffee&rdquo;,&ldquo;donuts&rdquo;,&ldquo;time&rdquo;,&ldquo;toffee&rdquo;]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The following selections are valid:</p> <ul> <li> <p>(&ldquo;coffee&rdquo;, &ldquo;donuts&rdquo;): The company name created is &ldquo;doffee conuts&rdquo;.</p> </li> <li> <p>(&ldquo;donuts&rdquo;, &ldquo;coffee&rdquo;): The company name created is &ldquo;conuts doffee&rdquo;.</p> </li> <li> <p>(&ldquo;donuts&rdquo;, &ldquo;time&rdquo;): The company name created is &ldquo;tonuts dime&rdquo;.</p> </li> <li> <p>(&ldquo;donuts&rdquo;, &ldquo;toffee&rdquo;): The company name created is &ldquo;tonuts doffee&rdquo;.</p> </li> <li> <p>(&ldquo;time&rdquo;, &ldquo;donuts&rdquo;): The company name created is &ldquo;dime tonuts&rdquo;.</p> </li> <li> <p>(&ldquo;toffee&rdquo;, &ldquo;donuts&rdquo;): The company name created is &ldquo;doffee tonuts&rdquo;.</p> </li> </ul> <p>Therefore, there are a total of 6 distinct company names.</p> <p>The following are some examples of invalid selections:</p> <ul> <li> <p>(&ldquo;coffee&rdquo;, &ldquo;time&rdquo;): The name &ldquo;toffee&rdquo; formed after swapping already exists in the original array.</p> </li> <li> <p>(&ldquo;time&rdquo;, &ldquo;toffee&rdquo;): Both names are still the same after swapping and exist in the original array.</p> </li> <li> <p>(&ldquo;coffee&rdquo;, &ldquo;toffee&rdquo;): Both names formed after swapping already exist in the original array.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> ideas = [&ldquo;lack&rdquo;,&ldquo;back&rdquo;]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= ideas[i].length <= 10</code></li> <li><code>ideas[i]</code> consists of lowercase English letters.</li> <li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • distinctNames

      public long distinctNames(String[] ideas)