Leetcode 6094、公司命名
解题思路
所有方法的本质就是根据首字母对字符串进行划分,在比较两个集合是否可以生成有效公司名字的时候,只需求两个集合之间交集的个数,
分别用两个集合的元素个数-交集个数,相乘,得到当前两个集合可以生成的有效公司名(单方向的,比如:A-B,并没有考虑B-A,
这个后面会讲)
下面的方法1就是根据首字母对集合进行区分,双重循环中的内层循环并没有从0开始,因为同样的两个单词只需要考虑一次,
最后在返回结果的时候把result * 2就可以得到正确的结果。
方法1、可以通过测试用例
代码
class Solution {
public long distinctNames(String[] ideas) {
Set<String>[] sets = new HashSet[26];
for(int i = 0; i < 26; i++)
sets[i] = new HashSet();
for(String idea : ideas) {
sets[idea.charAt(0) - 'a'].add(idea.substring(1));
}
long ans = 0;
for(int i = 0; i < 26; i++) {
for(int j = 0; j < i; j++) {
int m = 0;
for(String s : sets[i])
if(sets[j].contains(s))
m++;
ans += (long) (sets[i].size() - m) * (sets[j].size() - m);
}
}
return ans * 2;
}
}
下面的方法都是超出了时间限制
方法2,调用java中的API求两个集合之间的差集
/**
* 超出时间限制
* 求差集
* @param ideas
* @return
*/
public long distinctNames(String[] ideas) {
Map<String, Set<String>> map = new LinkedHashMap<>();
int len = ideas.length;
int result = 0;
for(int i = 0; i < len; i++) {
String curr = ideas[i];
String key = curr.substring(1, curr.length());
String value = curr.substring(0, 1);
map.putIfAbsent(key, new HashSet<>());
map.get(key).add(value);
}
String[] ketSet = map.keySet().toArray(new String[map.size()]);
for(int i = 0; i < ketSet.length; i++) {
for(int j = i + 1; j < ketSet.length; j++) {
Set<String> iSet = new HashSet<>(map.get(ketSet[i]));
Set<String> ii = new HashSet<>(map.get(ketSet[i]));
Set<String> jSet = new HashSet<>(map.get(ketSet[j]));
// removeAll方法是从iSet中删除jSet中出现过的元素,留下的就是差集。
boolean b = iSet.removeAll(jSet);
boolean c = jSet.removeAll(ii);
if(iSet.isEmpty() || jSet.isEmpty()) {
continue;
}else{
result += iSet.size() * jSet.size();
}
}
}
return result * 2;
}
方法3:暴力解法1,超出时间限制
双重循环ideas数组,每次比较两个字符串,调用swap交换两个字符串的首字母,生成两个新字符串,
看stringSet里面是否包含两个新字符串,如果都不包含,result += 2,因为A-B和B-A是两个结果。
public long distinctNames(String[] ideas) {
Set<String> stringSet = new HashSet<>();
long result = 0;
for(String idea : ideas) {
stringSet.add(idea);
}
for(int i = 0; i < ideas.length; i++) {
for(int j = i + 1; j < ideas.length; j++) {
if(ideas[i].equals(ideas[j])) {
continue;
}else {
String[] swap = swap(ideas[i], ideas[j]);
if(!stringSet.contains(swap[0]) && !stringSet.contains(swap[1])) {
result += 2;
}
}
}
}
return result;
}
public String[] swap(String s1, String s2) {
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();
char temp = chars1[0];
chars1[0] = chars2[0];
chars2[0] = temp;
return new String[]{new String(chars1), new String(chars2)};
}
方法4:暴力解法2,超出时间限制
使用map对idea根据除了首字母的后缀进行分组,value是首字母
在双重循环中访问两个字符串的时候,分别判断当前的首字母是否存在另一个集合里面
如果都不存在,result+=2
public long distinctNames(String[] ideas) {
Map<String, Set<String>> map = new HashMap<>();
int len = ideas.length;
int result = 0;
for(int i = 0; i < len; i++) {
String curr = ideas[i];
String key = curr.substring(1, curr.length());
String value = curr.substring(0, 1);
map.putIfAbsent(key, new HashSet<>());
map.get(key).add(value);
}
for(int i = 0; i < len; i++) {
String ii = ideas[i];
for(int j = i + 1; j < len; j++) {
String ij = ideas[j];
String key1 = ii.substring(1, ii.length());
String key2 = ij.substring(1, ij.length());
String value1 = ii.substring(0,1);
String value2 = ij.substring(0,1);
if(map.get(key1).contains(value2) || map.get(key2).contains(value1)) {
continue;
}else {
result += 2;
}
}
}
return result;
}
1284

被折叠的 条评论
为什么被折叠?



