File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
1
+ # naming a company | leetcode 2306 | https://leetcode.com/problems/naming-a-company
2
+ # bucket by starting character to make it n(26^2.n) and compare each set with each other
3
+
4
+ class Solution :
5
+ def distinctNames (self , ideas : list [str ]) -> int :
6
+ buckets = dict ()
7
+ num_distinct = 0
8
+
9
+ for idea in ideas :
10
+ if buckets .get (idea [0 ]) is None :
11
+ buckets [idea [0 ]] = {idea [1 :]}
12
+ else :
13
+ buckets [idea [0 ]].add (idea [1 :])
14
+
15
+ for prefix_i , suffix_i in buckets .items ():
16
+ for prefix_j , suffix_j in buckets .items ():
17
+ if prefix_i == prefix_j :
18
+ continue
19
+ common = len (suffix_i & suffix_j )
20
+ common_i = len (suffix_i ) - common
21
+ common_j = len (suffix_j ) - common
22
+ num_distinct += common_i * common_j
23
+
24
+ return num_distinct
You can’t perform that action at this time.
0 commit comments