Naming a Company

hard hash set grouping

Problem

You are given a list of distinct lowercase idea names. To build a company name you pick two different ideas, swap their first letters, and join the two swapped words with a space. The name is valid only if neither swapped word already appears in the original list. The two ideas are ordered, so "A B" and "B A" count separately.

Return the number of distinct valid company names you can form.

Inputideas = ["coffee","donuts","time","toffee"]
Output6
"coffee donuts", "donuts coffee", "time donuts", "donuts time", "toffee donuts", "donuts toffee" all work; swapping "coffee" with "toffee" fails because both swapped words already exist.

def distinct_names(ideas):
    groups = [set() for _ in range(26)]
    for name in ideas:
        groups[ord(name[0]) - ord('a')].add(name[1:])
    total = 0
    for a in range(26):
        for b in range(a + 1, 26):
            common = len(groups[a] & groups[b])
            total += 2 * (len(groups[a]) - common) * (len(groups[b]) - common)
    return total
function distinctNames(ideas) {
  const groups = Array.from({ length: 26 }, () => new Set());
  for (const name of ideas) {
    groups[name.charCodeAt(0) - 97].add(name.slice(1));
  }
  let total = 0;
  for (let a = 0; a < 26; a++) {
    for (let b = a + 1; b < 26; b++) {
      let common = 0;
      for (const s of groups[a]) if (groups[b].has(s)) common++;
      total += 2 * (groups[a].size - common) * (groups[b].size - common);
    }
  }
  return total;
}
long distinctNames(String[] ideas) {
    Set<String>[] groups = new HashSet[26];
    for (int i = 0; i < 26; i++) groups[i] = new HashSet<>();
    for (String name : ideas) {
        groups[name.charAt(0) - 'a'].add(name.substring(1));
    }
    long total = 0;
    for (int a = 0; a < 26; a++) {
        for (int b = a + 1; b < 26; b++) {
            int common = 0;
            for (String s : groups[a]) if (groups[b].contains(s)) common++;
            total += 2L * (groups[a].size() - common) * (groups[b].size() - common);
        }
    }
    return total;
}
long long distinctNames(vector<string>& ideas) {
    unordered_set<string> groups[26];
    for (auto& name : ideas) {
        groups[name[0] - 'a'].insert(name.substr(1));
    }
    long long total = 0;
    for (int a = 0; a < 26; a++) {
        for (int b = a + 1; b < 26; b++) {
            int common = 0;
            for (auto& s : groups[a]) if (groups[b].count(s)) common++;
            long long sa = (long long) groups[a].size() - common;
            total += 2 * sa * ((long long) groups[b].size() - common);
        }
    }
    return total;
}
Time: O(26 · n) Space: O(n)