Two strings X
and Y
are similar if we can swap two letters (in different positions) of X
, so that it equals Y
.
For example, "tars"
and "rats"
are similar (swapping at positions 0
and 2
), and "rats"
and "arts"
are similar, but "star"
is not similar to "tars"
, "rats"
, or "arts"
.
Together, these form two connected groups by similarity: {"tars", "rats", "arts"}
and {"star"}
. Notice that "tars"
and "arts"
are in the same group even though they are not similar. Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
We are given a list A
of unique strings. Every string in A
is an anagram of every other string in A
. How many groups are there?
Example 1:
Input: ["tars","rats","arts","star"] Output: 2
Note:
A.length <= 2000
A[i].length <= 1000
A.length * A[i].length <= 20000
- All words in
A
consist of lowercase letters only. - All words in
A
have the same length and are anagrams of each other. - The judging time limit has been increased for this question.
Union-find是肯定的,关键是怎么有效的构造连接矩阵
(1)两两遍历A中每个string,判断是不是similar,复杂度A.length*A.length*A[i].length
(2)吧A放到set里面,然后对A里面的每个string,两两交换A[i]里面字符的位置,然后判断是不是在set里面,复杂度A.length*A[i].length*A[i].length
因为LC上的hard case是A[i].length很大的情况,所以2 TLE,其实本题A.length与A[i]成正比的
package l839;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
class Solution {
public int numSimilarGroups(String[] a) {
if(a.length==0) return 0;
int n=a.length, m=a[0].length();
Map<String, Integer>d=new HashMap<String, Integer>();
for(int i=0;i<n;i++)d.put(a[i], i);
boolean[][]adj=new boolean[n][n];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++) {
int p=-1,q=-1;
boolean ok=true;
for(int k=0;k<m;k++)
if(a[i].charAt(k)!=a[j].charAt(k))
if(p==-1) p=k;
else if(q==-1) q=k;
else {ok=false;break;}
if(ok && a[i].charAt(p)==a[j].charAt(q) && a[i].charAt(q)==a[j].charAt(p))
adj[i][j]=true;
}
int[]p=new int[n];
for(int i=0;i<n;i++)p[i]=i;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(adj[i][j]) union(p,i,j);
Set<Integer>res=new HashSet<Integer>();
for(int i=0;i<n;i++)res.add(find(p,i));
return res.size();
}
private int find(int[] p, int i) {
while(p[i]!=i) i=p[i];
return i;
}
private void union(int[] p, int i, int j) {
i = find(p,i);
j = find(p,j);
p[i]=j;
}
}