LeetCode每日一题(2306. Naming a Company)

给定一个字符串数组,用于创建公司名称。选取两个不同的名字,交换它们的首字母,如果新名字都不在原始数组中,则是有效公司名。返回有效的公司名数量。题目要求计算并返回所有可能的有效公司名组合数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
Swap the first letters of ideaA and ideaB with each other.
If both of the new names are not found in the original ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.
Otherwise, it is not a valid name.
Return the number of distinct valid names for the company.

Example 1:

Input: ideas = [“coffee”,“donuts”,“time”,“toffee”]
Output: 6

Explanation: The following selections are valid:

  • (“coffee”, “donuts”): The company name created is “doffee conuts”.
  • (“donuts”, “coffee”): The company name created is “conuts doffee”.
  • (“donuts”, “time”): The company name created is “tonuts dime”.
  • (“donuts”, “toffee”): The company name created is “tonuts doffee”.
  • (“time”, “donuts”): The company name created is “dime tonuts”.
  • (“toffee”, “donuts”): The company name created is “doffee tonuts”.
    Therefore, there are a total of 6 distinct company names.

The following are some examples of invalid selections:

  • (“coffee”, “time”): The name “toffee” formed after swapping already exists in the original array.
  • (“time”, “toffee”): Both names are still the same after swapping and exist in the original array.
  • (“coffee”, “toffee”): Both names formed after swapping already exist in the original array.

Example 2:

Input: ideas = [“lack”,“back”]
Output: 0

Explanation: There are no valid selections. Therefore, 0 is returned.

Constraints:

  • 2 <= ideas.length <= 5 * 104
  • 1 <= ideas[i].length <= 10
  • ideas[i] consists of lowercase English letters.
  • All the strings in ideas are unique.

设立一个 counts[26][26]来保存所有 idea 的首字母可以合法转换的途径数量, 比如 counts[‘a’][‘b’]代表首字母是’a’而且可以合法转换成’b’的 idea 的数量, 这里说的合法其实就一个标准, 即转换之后的 idea 在 ideas 中不存在, 我们有了 counts 后, 计算答案就变得简单了, 我们任意取两个首字母, 比如说’a’, ‘b’, counts[‘a’][‘b’]代表首字母为’a’并且可以转化为’b’的 idea 数量, counts[‘b’][‘a’]为首字母为’b’并且可以转化为’a’的 idea 的数量, counts[‘a’][‘b’] * counts[‘b’][‘a’]就是首字母为’a’和首字母为’b’的 ideas 合法组合的数量。


use std::collections::HashSet;

impl Solution {
    pub fn distinct_names(ideas: Vec<String>) -> i64 {
        let all: HashSet<Vec<char>> = ideas.iter().map(|s| s.chars().collect()).collect();
        let mut counts = vec![vec![0i64; 26]; 26];
        for idea in &all {
            let mut chars = idea.clone();
            for c in 97..123u8 {
                chars[0] = c as char;
                if !all.contains(&chars) {
                    counts[idea[0] as usize - 97][c as usize - 97] += 1;
                }
            }
        }
        let mut ans = 0;
        for c1 in 0..26 {
            for c2 in 0..26 {
                ans += counts[c1][c2] * counts[c2][c1];
            }
        }
        ans
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值