839. Similar String Groups

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:

  1. A.length <= 2000
  2. A[i].length <= 1000
  3. A.length * A[i].length <= 20000
  4. All words in A consist of lowercase letters only.
  5. All words in A have the same length and are anagrams of each other.
  6. 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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值