website/content.en/ChapterFour/0800~0899/0839.Similar-String-Groups.md
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 <= 2000A[i].length <= 1000A.length * A[i].length <= 20000A consist of lowercase letters only.A have the same length and are anagrams of each other.If we swap two letters at different positions in string X so that it becomes equal to string Y, then X and Y are called similar strings.
For example, "tars" and "rats" are similar (swap positions 0 and 2); "rats" and "arts" are also similar, but "star" is not similar to "tars", "rats", or "arts".
Together, they form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Note that "tars" and "arts" are in the same group even though they are not similar. Formally, for each group, to determine whether a word is in the group, it only needs to be similar to at least one word in that group. We are given a list A of strings without duplicates. Every string in the list is an anagram of every other string in A. How many similar string groups are there in A?
Hints:
Remark:
anagrams (anagram means any permutation and combination of the string). Then this problem becomes relatively simple: we only need to determine whether every two strings are "similar"; if they are similar, perform union(), and finally check how many sets there are in the Union Find.
package leetcode
import (
"github.com/halfrost/leetcode-go/template"
)
func numSimilarGroups(A []string) int {
uf := template.UnionFind{}
uf.Init(len(A))
for i := 0; i < len(A); i++ {
for j := i + 1; j < len(A); j++ {
if isSimilar(A[i], A[j]) {
uf.Union(i, j)
}
}
}
return uf.TotalCount()
}
func isSimilar(a, b string) bool {
var n int
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
n++
if n > 2 {
return false
}
}
}
return true
}