LeetCode839. Similar String Groups——并查集

文章目录

一、题目

Two strings, X and Y, are considered similar if either they are identical or we can make them equivalent by swapping at most two letters (in distinct positions) within the string X.

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 strs of strings where every string in strs is an anagram of every other string in strs. How many groups are there?

Example 1:

Input: strs = [“tars”,“rats”,“arts”,“star”]
Output: 2
Example 2:

Input: strs = [“omv”,“ovm”]
Output: 1

Constraints:

1 <= strs.length <= 300
1 <= strs[i].length <= 300
strs[i] consists of lowercase letters only.
All words in strs have the same length and are anagrams of each other.

二、题解

class Solution {
   
public:
    int father[301];
    int nums;
    int numSimilarGroups(vector<string>& strs) {
   
        int n = strs.size();
        int m = strs[0].size();
        build(n);
        for(int i = 0;i < n;i++){
   
            for(int j = i + 1;j < n;j++){
   
                if(find(i) != find(j)){
   
                    int diff = 0;
                    for(int k = 0;k < m && diff < 3;k++){
   
                        if(strs[i][k] != strs[j][k]) diff++;
                    }
                    if(diff == 0 || diff == 2) unions(i,j);
                }
            }
        }
        return nums;
    }
    void build(int m){
   
        for(int i = 0;i < m;i++){
   
            father[i] = i;
        }
        nums = m;
    }
    int find(int x){
   
        if(x != father[x]) father[x] = find(father[x]);
        return father[x];
    }
    void unions(int x,int y){
   
        int fx = find(x),fy = find(y);
        if(fx != fy){
   
            father[fx] = fy;
            nums--;
        }
    }
};

相关推荐

  1. LeetCode839. Similar String Groups——

    2024-02-01 23:38:04       53 阅读
  2. leetcode

    2024-02-01 23:38:04       35 阅读
  3. LeetCode 721. 账户合并

    2024-02-01 23:38:04       24 阅读
  4. LeetCode765. Couples Holding Hands——

    2024-02-01 23:38:04       43 阅读
  5. LeetCode2092. Find All People With Secret——

    2024-02-01 23:38:04       46 阅读
  6. Leetcode/DFS/BFS多解

    2024-02-01 23:38:04       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 23:38:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 23:38:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 23:38:04       82 阅读
  4. Python语言-面向对象

    2024-02-01 23:38:04       91 阅读

热门阅读

  1. Python 机器学习 K-近邻算法 常用距离度量方法

    2024-02-01 23:38:04       58 阅读
  2. Android String.format() 引发的卡顿问题

    2024-02-01 23:38:04       53 阅读
  3. Vue2项目中实现头像上传

    2024-02-01 23:38:04       54 阅读
  4. C# 递归执行顺序

    2024-02-01 23:38:04       50 阅读
  5. C#面:sealed修饰符有什么特点

    2024-02-01 23:38:04       52 阅读
  6. mybatis一对多查询,list中的泛型是包装类

    2024-02-01 23:38:04       48 阅读
  7. DynamoDB 的 LSI 和 GSI 有什么区别?

    2024-02-01 23:38:04       53 阅读
  8. Linux

    Linux

    2024-02-01 23:38:04      48 阅读
  9. 每日OJ题_算法_前缀和⑦_力扣525. 连续数组

    2024-02-01 23:38:04       74 阅读