Skip to content

Latest commit

 

History

History
212 lines (174 loc) · 5.15 KB

File metadata and controls

212 lines (174 loc) · 5.15 KB

中文文档

Description

Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.

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.

Solutions

Python3

class Solution:
    def numSimilarGroups(self, strs: List[str]) -> int:
        n = len(strs)
        p = list(range(n))

        def find(x):
            if p[x] != x:
                p[x] = find(p[x])
            return p[x]

        def check(a, b):
            cnt = 0
            for i, c in enumerate(a):
                if c != b[i]:
                    cnt += 1
            return cnt <= 2

        for i in range(n):
            for j in range(i + 1, n):
                if check(strs[i], strs[j]):
                    p[find(i)] = find(j)

        return sum(i == find(i) for i in range(n))

Java

class Solution {
    private int[] p;

    public int numSimilarGroups(String[] strs) {
        int n = strs.length;
        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 (check(strs[i], strs[j])) {
                    p[find(i)] = find(j);
                }
            }
        }
        int res = 0;
        for (int i = 0; i < n; ++i) {
            if (i == find(i)) {
                ++res;
            }
        }
        return res;
    }

    private boolean check(String a, String b) {
        int cnt = 0;
        int n = a.length();
        for (int i = 0; i < n; ++i) {
            if (a.charAt(i) != b.charAt(i)) {
                ++cnt;
            }
        }
        return cnt <= 2;
    }

    private int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

C++

class Solution {
public:
    vector<int> p;
    int numSimilarGroups(vector<string>& strs) {
        int n = strs.size();
        for (int i = 0; i < n; ++i) p.push_back(i);
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                if (check(strs[i], strs[j]))
                    p[find(i)] = find(j);
            }
        }
        int res = 0;
        for (int i = 0; i < n; ++i)
        {
            if (i == find(i)) ++res;
        }
        return res;
    }

    bool check(string a, string b) {
        int cnt = 0;
        int n = a.size();
        for (int i = 0; i < n; ++i)
            if (a[i] != b[i]) ++cnt;
        return cnt <= 2;
    }

    int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
};

Go

var p []int

func numSimilarGroups(strs []string) int {
	n := len(strs)
	p = make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if !check(strs[i], strs[j]) {
				continue
			}
			p[find(i)] = find(j)
		}
	}
	res := 0
	for i := 0; i < n; i++ {
		if i == find(i) {
			res++
		}
	}
	return res
}

func check(a, b string) bool {
	cnt, n := 0, len(a)
	for i := 0; i < n; i++ {
		if a[i] != b[i] {
			cnt++
		}
	}
	return cnt <= 2
}

func find(x int) int {
	if p[x] != x {
		p[x] = find(p[x])
	}
	return p[x]
}

...