Naming a Company LeetCode solution in Python

1
class Solution:
2
def distinctNames(self, ideas: List[str]) -> int:
3

4
names = defaultdict(set)
5
res = 0
6

7
# to store first letter as key and followed suffix as val
8
for i in ideas:
9
names[i[0]].add(i[1:])
10

11
# list of distinct first-letters available in ideas (may or may not contain all alphabets,depends upon elements in ideas)
12
arr = list(names.keys())
13
ans, n = 0, len(arr)
14

15
for i in range(n):
16
for j in range(i + 1, n):
17
# a,b => 2 distinct first letters
18
a, b = arr[i], arr[j]
19
# adding the number of distinct posssible suffixes and multiplying by 2 as the new word formed might be "newword1 newword2" or "newword2 newword1"
20
res += len(names[a] - names[b]) * len(names[b] - names[a]) * 2
21

22
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0