Naming a Company
You are given an array of strings ideas
-
Choose 2 distinct names from
, call themideas andideaA .ideaB -
Swap the first letters of
andideaA with each other.ideaB -
If both of the new names are not found in the original
, then the nameideasideaA (the concatenation ofideaB andideaA , separated by a space) is a valid company name.ideaB - Otherwise, it is not a valid name.
Return the number of distinct valid names for the company.
Example 1
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
Output: 0
Explanation: There are no valid selections. Therefore, 0 is returned.
Constraints:
2 <= ideas.length <= 5 * 1041 <= ideas[i].length <= 10 consists of lowercase English letters.ideas[i]- All the strings in
are unique.ideas
Solution 1 - Group by starting letter of idea word and store distinct ideas into HashSet
The main intution behind this solution is that, since we have to swap 1st character only and find if that resultant word exists in original input array of ideas"coffee""toffee"'c''t''t''c'idea
So, in this approach, we will group the words by their starting letter into a HashSetideasSet26'a''z'HashSet1
Then, we will loop through each entry in ideasSet
| Starting letter | Group by set |
|---|---|
| c | offee |
| d | onut |
| t | ime |
| offee |
'c''t''c''t'1"coffee""time"So the number unique combinations can be formed are
words size starts with 'a' words exists in both setswords size starts with 't' words exists in both setsimport java.util.HashSet;
public class NamingACompany {
static long distinctNames(String[] ideas) {
HashSet<String>[] set = new HashSet[26];
for(String idea: ideas) {
int index = idea.charAt(0) - 'a';
if(set[index] == null) {
set[index] = new HashSet<>();
}
set[index].add(idea.substring(1));
}
long result = 0;
for(int i=0; i<set.length; i++) {
HashSet<String> ideasASet = set[i];
if(ideasASet == null) {
continue;
}
for(int j=i; j<set.length; j++) {
HashSet<String> ideasBSet = set[j];
if(ideasBSet == null) {
continue;
}
int existInBoth = 0; // suffix words that exits in both sets
for(String ideasB: ideasBSet) {
if(ideasASet.contains(ideasB)) {
existInBoth++;
}
}
result += (ideasBSet.size() - existInBoth) * (ideasASet.size() - existInBoth) * 2L;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(distinctNames(new String[]{"coffee","donuts","time","toffee"}));
System.out.println(distinctNames(new String[]{"lack","back"}));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where nideasn
Space complexity: O(n), since we are storing words into array of HashSets.
Above implementations source code can be found at