g2301_2400.s2306_naming_a_company.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2306_naming_a_company;
// #Hard #Array #String #Hash_Table #Bit_Manipulation #Enumeration
// #2022_06_15_Time_486_ms_(74.14%)_Space_83.3_MB_(58.98%)
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 2306 - Naming a Company\.
*
* Hard
*
* 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:
*
* 1. Choose 2 **distinct** names from `ideas`, call them ideaA
and ideaB
.
* 2. Swap the first letters of ideaA
and ideaB
with each other.
* 3. 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.
* 4. 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**.
**/
public class Solution {
private long count(Map> map, char a, char b) {
if (!map.containsKey(a) || !map.containsKey(b)) {
return 0;
}
long common = 0;
Set first = map.get(a);
Set second = map.get(b);
for (String c : first) {
if (second.contains(c)) {
common++;
}
}
long uniqueA = first.size() - common;
long uniqueB = second.size() - common;
return uniqueA * uniqueB * 2L;
}
public long distinctNames(String[] ideas) {
long ans = 0;
Map> map = new HashMap<>();
for (String idea : ideas) {
char startChar = idea.charAt(0);
Set values = map.getOrDefault(startChar, new HashSet<>());
values.add(idea.substring(1));
map.put(startChar, values);
}
for (int i = 0; i <= 26; i++) {
for (int j = i + 1; j <= 26; j++) {
long unique = count(map, (char) (i + 'a'), (char) (j + 'a'));
ans += unique;
}
}
return ans;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy