2306. Naming a Company
https://leetcode.com/problems/naming-a-company/
Python
from collections import defaultdict
from string import ascii_lowercase
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
mapper = defaultdict(set)
for idea in ideas:
mapper[idea[0]].add(idea[1:])
ans = 0
for i in range(25):
for j in range(i+1, 26):
prefix1 = ascii_lowercase[i]
prefix2 = ascii_lowercase[j]
mutuals = len(mapper[prefix1] & mapper[prefix2])
ans += 2 * (len(mapper[prefix1]) - mutuals) * (len(mapper[prefix2]) - mutuals)
return ans