[leetcode] 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 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.

DFS

思路

先放下如何判定“相似”的问题,题目中,如果两个字符串相似,那么他们可以被分到同一个组里。问题启发我们构造图,若两个字符串相似,那么在图中有这两个节点有边相连。问题便转化为一个求图中连通分量的问题了。

代码

class Solution {
public:
    int numSimilarGroups(vector<string>& A) {
        typedef struct {
            string& s;
            vector<int> next;
        } Node;
        vector<Node> graph;
        for(auto iter = A.begin(); iter != A.end(); ++iter) {
            graph.push_back(Node{ *iter, vector<int>() });
        }
        for(int i = 0; i != A.size(); ++i) {
            for(int j = i+1; j != A.size(); ++j) {
                if(isSimilar(A[i], A[j])) {
                    graph[i].next.push_back(j);
                    graph[j].next.push_back(i);
                }
            }
        }
        function<void (int)> dfs;
        vector<bool> hasVisited(A.size(), false);
        dfs = [&dfs, &hasVisited, &graph] (int begin) {
            if(hasVisited[begin]) {
                return;
            }
            hasVisited[begin] = true;
            for(auto i : graph[begin].next) {
                dfs(i);
            }
        };
        int groupCount = 0;
        for(int i = 0; i != A.size(); ++i) {
            if(!hasVisited[i]) {
                ++groupCount;
                dfs(i);
            }
        }
        return groupCount;
    }
private:
    bool isSimilar(string& a, string& b) {
        if(a.size() != b.size()) {
            return false;
        }
        int n = a.size();
        vector<int> diff;
        for(int i = 0; i != n; ++i) {
            if(a[i] != b[i]) {
                diff.push_back(i);
            }
        }
        if(diff.size() == 2) {
            return a[diff[0]] == b[diff[1]]
                && a[diff[1]] == b[diff[0]];
        } else if(diff.size() == 0) {
            vector<bool> appear(26, false);
            for(int i = 0; i != n; ++i) {
                if(a[i] == b[i]) {
                    if(appear[a[i] - 'a']) {
                        return true;
                    } else {
                        appear[a[i] - 'a'] = true;
                    }

                }
            }
        }
        return false;
    };
};  };
        int groupCount = 0;
        for(int i = 0; i != A.size(); ++i) {
            if(!hasVisited[i]) {
                ++groupCount;
                dfs(i);
            }
        }
        return groupCount;
    }
};

并查集

事实上,处理这种图中有多少分量的问题,一种更优雅的方式是使用并查集,这样子函数会有下面的形式。

int numSimilarGroups(vector<string>& A) {
    disjoint_set ds(A.size());
    for (int i = 0; i < A.size(); i++)
        for (int j = i + 1; j < A.size(); j++)
            if (isSimilar(A[i], A[j]))
                ds.join(i, j);
    return ds.size();
}

其中并查集的定义为:

class disjoint_set {
    vector<int> v;
    int sz;
public:
    disjoint_set(int n) {
        makeset(n);
    }

    void makeset(int n) {
        v.resize(n);
        iota(v.begin(), v.end(), 0);
        sz = n;
    }

    int find(int i) {
        if (i != v[i])
            v[i] = find(v[i]);
        return v[i];
    }
    
    void join(int i, int j) {
        int ri = find(i), rj = find(j);
        if (ri != rj) {
            v[ri] = rj;
            sz--;
        }
    }
    
    int size() {
        return sz;
    }
};

时间复杂度

算法的复杂度主要部分为算法中的二重循环,复杂度均为 O ( n 2 ) O(n^2) O(n2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值