<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> ideas = [&quot;coffee&quot;,&quot;donuts&quot;,&quot;time&quot;,&quot;toffee&quot;]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- (&quot;coffee&quot;, &quot;donuts&quot;): The company name created is &quot;doffee conuts&quot;.
- (&quot;donuts&quot;, &quot;coffee&quot;): The company name created is &quot;conuts doffee&quot;.
- (&quot;donuts&quot;, &quot;time&quot;): The company name created is &quot;tonuts dime&quot;.
- (&quot;donuts&quot;, &quot;toffee&quot;): The company name created is &quot;tonuts doffee&quot;.
- (&quot;time&quot;, &quot;donuts&quot;): The company name created is &quot;dime tonuts&quot;.
- (&quot;toffee&quot;, &quot;donuts&quot;): The company name created is &quot;doffee tonuts&quot;.
Therefore, there are a total of 6 distinct company names.

The following are some examples of invalid selections:
- (&quot;coffee&quot;, &quot;time&quot;): The name &quot;toffee&quot; formed after swapping already exists in the original array.
- (&quot;time&quot;, &quot;toffee&quot;): Both names are still the same after swapping and exist in the original array.
- (&quot;coffee&quot;, &quot;toffee&quot;): Both names formed after swapping already exist in the original array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> ideas = [&quot;lack&quot;,&quot;back&quot;]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= ideas.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= ideas[i].length &lt;= 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>

class Solution:
    def distinctNames(self, ideas: List[str]) -> int:
        """
        Calculate the number of distinct valid company names that can be formed by swapping
        the first letters of any two distinct company names.

        Args:
        ideas (List[str]): A list of company names.

        Returns:
        int: The number of distinct valid company names that can be formed.

        Time Complexity: O(n), where "n" is the number of words in the input list.
        Space Complexity: O(n), where "n" is the number of words in the input list.
        """
        # Map to store the suffixes of words grouped by their first character
        prefix_to_suffix_set = defaultdict(set)
        total_distinct_names = 0

        # Populate the map with suffixes grouped by their first letter
        for word in ideas:
            first_letter = word[0]
            suffix = word[1:]
            prefix_to_suffix_set[first_letter].add(suffix)

        # Iterate over each pair of different first characters
        for first_char_a in prefix_to_suffix_set:
            for first_char_b in prefix_to_suffix_set:
                if first_char_a == first_char_b:
                    continue  # Skip if both first characters are the same

                # Calculate the intersection of suffixes between two different first letters
                common_suffix_count = 0
                for suffix in prefix_to_suffix_set[first_char_a]:
                    if suffix in prefix_to_suffix_set[first_char_b]:
                        common_suffix_count += 1

                # Calculate the distinct suffixes after removing the intersection
                distinct_suffix_a = len(prefix_to_suffix_set[first_char_a]) - common_suffix_count
                distinct_suffix_b = len(prefix_to_suffix_set[first_char_b]) - common_suffix_count

                # Multiply the distinct suffix counts and add to the total distinct names count
                total_distinct_names += distinct_suffix_a * distinct_suffix_b
        # Return the total distinct names count
        return total_distinct_names

# time complexity: O(n) where "n" is the number of words in the input list.
# space complexity: O(n) where "n" is the number of words in the input list.
689