LeetCode 题解工作台

公司命名

给你一个字符串数组 ideas 表示在公司命名过程中使用的名字列表。公司命名流程如下: 从 ideas 中选择 2 个 不同 名字,称为 idea A 和 idea B 。 交换 idea A 和 idea B 的首字母。 如果得到的两个新名字 都 不在 ideas 中,那么 idea A idea…

category

5

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 数组·哈希·扫描

bolt

答案摘要

我们定义 表示 中以第 个字母开头,替换为第 个字母后,不在 中的字符串的个数。初始时 $f[i][j] = 0$。另外,用一个哈希表 记录 中的字符串,方便我们快速判断某个字符串是否在 中。 接下来,我们遍历 中字符串,对于当前遍历到的字符串 ,我们枚举替换后的第一个字母 ,如果 替换后的字符串不在 中,那么我们就更新 $f[i][j] = f[i][j] + 1$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串数组 ideas 表示在公司命名过程中使用的名字列表。公司命名流程如下:

  1. ideas 中选择 2 个 不同 名字,称为 ideaAideaB
  2. 交换 ideaAideaB 的首字母。
  3. 如果得到的两个新名字 不在 ideas 中,那么 ideaA ideaB串联 ideaAideaB ,中间用一个空格分隔)是一个有效的公司名字。
  4. 否则,不是一个有效的名字。

返回 不同 且有效的公司名字的数目。

 

示例 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 * 104
  • 1 <= ideas[i].length <= 10
  • ideas[i] 由小写英文字母组成
  • ideas 中的所有字符串 互不相同
lightbulb

解题思路

方法一:枚举计数

我们定义 f[i][j]f[i][j] 表示 ideas\textit{ideas} 中以第 ii 个字母开头,替换为第 jj 个字母后,不在 ideas\textit{ideas} 中的字符串的个数。初始时 f[i][j]=0f[i][j] = 0。另外,用一个哈希表 ss 记录 ideas\textit{ideas} 中的字符串,方便我们快速判断某个字符串是否在 ideas\textit{ideas} 中。

接下来,我们遍历 ideas\textit{ideas} 中字符串,对于当前遍历到的字符串 vv,我们枚举替换后的第一个字母 jj,如果 vv 替换后的字符串不在 ideas\textit{ideas} 中,那么我们就更新 f[i][j]=f[i][j]+1f[i][j] = f[i][j] + 1

最后,我们再次遍历 ideas\textit{ideas} 中字符串,对于当前遍历到的字符串 vv,我们枚举替换后的第一个字母 jj,如果 vv 替换后的字符串不在 ideas\textit{ideas} 中,那么我们就更新答案 ans=ans+f[j][i]\textit{ans} = \textit{ans} + f[j][i]

最终答案即为 ans\textit{ans}

时间复杂度 O(n×m×Σ)O(n \times m \times |\Sigma|),空间复杂度 O(Σ2)O(|\Sigma|^2)。其中 nnmm 分别是 ideas\textit{ideas} 中字符串的个数和字符串的最大长度,而 Σ|\Sigma| 是字符串中出现的字符集,本题中 Σ26|\Sigma| \leq 26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

公司命名题解:数组·哈希·扫描 | LeetCode #2306 困难