Naming a Company — LeetCode 2306 Python Solution
- Problem
- #2306
- Pattern
- Bit Manipulation
- Reading time
- 4 min
- Source
- leetcode.com
The problem
You are given an array of strings ideas 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: Choose 2 distinct names from ideas, call them ideaA and ideaB.
Example
- Input
- ideas = ["coffee","donuts","time","toffee"]
- Output
- 6
- Explanation
- The following selections are valid:
Python solution
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
s = set(ideas)
f = [[0] * 26 for _ in range(26)]
for v in ideas:
i = ord(v[0]) - ord('a')
t = list(v)
for j in range(26):
t[0] = chr(ord('a') + j)
if ''.join(t) not in s:
f[i][j] += 1
ans = 0
for v in ideas:
i = ord(v[0]) - ord('a')
t = list(v)
for j in range(26):
t[0] = chr(ord('a') + j)
if ''.join(t) not in s:
ans += f[j][i]
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times m \times |\Sigma|) |
| Space | O(|\Sigma|^2) auxiliary |
Pattern: Bit Manipulation
Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2306. Naming a Company is filed here because LeetCode tags it Bit Manipulation, which is the vocabulary this hub collects.
The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2306. Naming a Company?
- LeetCode 2306. Naming a Company is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2306. Naming a Company?
- The Python solution on this page runs in O(n \times m \times |\Sigma|).
- What is the space complexity of LeetCode 2306. Naming a Company?
- The Python solution on this page uses O(|\Sigma|^2) auxiliary space.
- What topics does LeetCode 2306. Naming a Company cover?
- LeetCode 2306. Naming a Company is tagged Bit Manipulation, Array, Hash Table, String and Enumeration on LeetCode.