LeetCode 题解工作台
公司命名
给你一个字符串数组 ideas 表示在公司命名过程中使用的名字列表。公司命名流程如下: 从 ideas 中选择 2 个 不同 名字,称为 idea A 和 idea B 。 交换 idea A 和 idea B 的首字母。 如果得到的两个新名字 都 不在 ideas 中,那么 idea A idea…
5
题型
4
代码语言
3
相关题
当前训练重点
困难 · 数组·哈希·扫描
答案摘要
我们定义 表示 中以第 个字母开头,替换为第 个字母后,不在 中的字符串的个数。初始时 $f[i][j] = 0$。另外,用一个哈希表 记录 中的字符串,方便我们快速判断某个字符串是否在 中。 接下来,我们遍历 中字符串,对于当前遍历到的字符串 ,我们枚举替换后的第一个字母 ,如果 替换后的字符串不在 中,那么我们就更新 $f[i][j] = f[i][j] + 1$。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一个字符串数组 ideas 表示在公司命名过程中使用的名字列表。公司命名流程如下:
- 从
ideas中选择 2 个 不同 名字,称为ideaA和ideaB。 - 交换
ideaA和ideaB的首字母。 - 如果得到的两个新名字 都 不在
ideas中,那么ideaA ideaB(串联ideaA和ideaB,中间用一个空格分隔)是一个有效的公司名字。 - 否则,不是一个有效的名字。
返回 不同 且有效的公司名字的数目。
示例 1:
输入:ideas = ["coffee","donuts","time","toffee"]
输出:6
解释:下面列出一些有效的选择方案:
- ("coffee", "donuts"):对应的公司名字是 "doffee conuts" 。
- ("donuts", "coffee"):对应的公司名字是 "conuts doffee" 。
- ("donuts", "time"):对应的公司名字是 "tonuts dime" 。
- ("donuts", "toffee"):对应的公司名字是 "tonuts doffee" 。
- ("time", "donuts"):对应的公司名字是 "dime tonuts" 。
- ("toffee", "donuts"):对应的公司名字是 "doffee tonuts" 。
因此,总共有 6 个不同的公司名字。
下面列出一些无效的选择方案:
- ("coffee", "time"):在原数组中存在交换后形成的名字 "toffee" 。
- ("time", "toffee"):在原数组中存在交换后形成的两个名字。
- ("coffee", "toffee"):在原数组中存在交换后形成的两个名字。
示例 2:
输入:ideas = ["lack","back"] 输出:0 解释:不存在有效的选择方案。因此,返回 0 。
提示:
2 <= ideas.length <= 5 * 1041 <= ideas[i].length <= 10ideas[i]由小写英文字母组成ideas中的所有字符串 互不相同
解题思路
方法一:枚举计数
我们定义 表示 中以第 个字母开头,替换为第 个字母后,不在 中的字符串的个数。初始时 。另外,用一个哈希表 记录 中的字符串,方便我们快速判断某个字符串是否在 中。
接下来,我们遍历 中字符串,对于当前遍历到的字符串 ,我们枚举替换后的第一个字母 ,如果 替换后的字符串不在 中,那么我们就更新 。
最后,我们再次遍历 中字符串,对于当前遍历到的字符串 ,我们枚举替换后的第一个字母 ,如果 替换后的字符串不在 中,那么我们就更新答案 。
最终答案即为 。
时间复杂度 ,空间复杂度 。其中 和 分别是 中字符串的个数和字符串的最大长度,而 是字符串中出现的字符集,本题中 。
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 ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
The candidate demonstrates understanding of hash table operations and can apply them to solve array-related problems efficiently.
- question_mark
The candidate identifies key edge cases, such as ensuring valid name combinations and avoiding duplicates.
- question_mark
The candidate suggests optimizations based on the size of the input and efficiently handles larger datasets.
常见陷阱
外企场景- error
Forgetting to filter out invalid pairs that are formed by identical swapped names.
- error
Not handling edge cases where the name formed by swapping already exists in the original list.
- error
Overcomplicating the solution or using nested loops inefficiently instead of hash lookups.
进阶变体
外企场景- arrow_right_alt
Consider solving this problem by optimizing the space complexity with a more compact representation of name prefixes.
- arrow_right_alt
Explore an approach that minimizes redundant checks when evaluating pairs with common prefix letters.
- arrow_right_alt
Modify the problem to allow case sensitivity or additional string modifications, such as swapping middle characters.