一刷311-剑指 Offer II 117. 相似的字符串h(839. 相似字符串组)

题目:
如果交换字符串 X 中的两个不同位置的字母,使得它和字符串 Y 相等,那么称 XY 两个字符串相似。
如果这两个字符串本身是相等的,那它们也是相似的。

例如,"tars""rats" 是相似的 (交换 02 的位置)"rats""arts" 也是相似的,
但是 "star" 不与 "tars""rats",或 "arts" 相似。

总之,它们通过相似性形成了两个关联组:{"tars", "rats", "arts"}{"star"}。
注意,"tars""arts" 是在同一组中,即使它们并不相似。
形式上,对每个组而言,要确定一个单词在组中,只需要这个词和该组中至少一个单词相似。

给定一个字符串列表 strs。列表中的每个字符串都是 strs 中其它所有字符串的一个 字母异位词 。
请问 strs 中有多少个相似字符串组?

字母异位词(anagram),一种把某个字符串的字母的位置(顺序)加以改换所形成的新词。
-----------------
示例 1:
输入:strs = ["tars","rats","arts","star"]
输出:2
示例 2:

输入:strs = ["omv","ovm"]
输出:1
 
提示:
1 <= strs.length <= 300
1 <= strs[i].length <= 300
strs[i] 只包含小写字母。
strs 中的所有单词都具有相同的长度,且是彼此的字母异位词。
-----------------------
思路:
中规中矩并查集。只要熟悉并查集写法,这题应该归属easy级别。

官解总是在得到所有不相交集后再循环一遍统计集合数,这点其实毫无必要。
可以设置一个mergedCount,每发生一次合并就mergedCount++,最后答案就是n - mergedCount,
n是一开始的单元素集合数量,也就是strs的大小。当然也可以在发生合并的时候n--,
最后返回n,道理是一样的。
-------------
class Solution {
    int mergedCount = 0;
    public int numSimilarGroups(String[] strs) {
        int n = strs.length;
        UnionFind uf = new UnionFind(n);
        for(int i = 0; i < n; i++){ // 两个for,考察任意两个字符串
            for(int j = i + 1; j < n; j++){
                String si = strs[i], sj = strs[j];
                if(uf.find(i) == uf.find(j)) continue; // 先判断是否已经在同一集合
                if(isSimilar(si, sj)) uf.union(i, j); // 若不在,且相似,则合并
            }
        }
        return n - mergedCount;
    }
    private boolean isSimilar(String s, String t){
        int n = s.length();
        int diff = 0;
        for(int i = 0; i < n; i++){
            if(s.charAt(i) != t.charAt(i)) diff++;
        } 
        return diff > 2 ? false : true;
    }
    private class UnionFind{
        int[] parent;
        int[] rank;
        public UnionFind(int n){
            this.parent = new int[n];
            this.rank = new int[n];
            for(int i = 0; i < n; i++){
                parent[i] = i;
                rank[i] = 1;
            }
        }
        public int find(int x){
            if(parent[x] == x) return x;
            return parent[x] = find(parent[x]);
        }
        public void union(int x, int y){
            int xRoot = find(x);
            int yRoot = find(y);
            if(xRoot != yRoot){
                mergedCount++; // 发生合并时,mergedCount立即加1
                if(rank[xRoot] <= rank[yRoot]) parent[yRoot] = xRoot;
                else parent[xRoot] = yRoot;
                if(rank[xRoot] == rank[yRoot]) rank[xRoot]++;
            }
        }
    }
}

LC
并查集

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值