LeetCode-839. Similar String Groups-Analysis and Code (Java)

LeetCode-839. Similar String Groups [Similar String Groups]-Analysis and Code [Java]

1. Topic

If the letters in two different positions in the string X are exchanged to make it equal to the string Y, then the two strings X and Y are said to be similar. If the two strings themselves are equal, then they are also similar.
For example, "tars" and "rats" are similar (exchange the positions of 0 and 2); "rats" and "arts" are also similar, but "star" is not the same as "tars", "rats", or "arts" similar.
In short, they formed two association groups through similarity: {"tars", "rats", "arts"} and {"star"}. Note that "tars" and "arts" are in the same group, even if they are not similar. Formally, for each group, to determine that a word is in the group, it only needs to be similar to at least one word in the group.
Gives you a list of strings strs. Each string in the list is a letter dyslexia of all other strings in strs. How many similar string groups are there in strs?

Example 1:

输入:strs = ["tars","rats","arts","star"]
输出:2

Example 2:

输入:strs = ["omv","ovm"]
输出:1

prompt:

  • 1 <= strs.length <= 100
  • 1 <= strs[i].length <= 1000
  • sum(strs[i].length) <= 2 * 104
  • strs[i] contains only lowercase letters.
  • All the words in strs have the same length and are diaphanous words of each other.

Remarks: anagram, a new word formed by changing the position (order) of the letters of a string.

Source: LeetCode
Link: https://leetcode-cn.com/problems/similar-string-groups
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Combine

(1) Thinking

Record the association relationship between the strings through the combined check and count the number of association groups.

(2) Code

class Solution {
    
    
    public int numSimilarGroups(String[] strs) {
    
    
        int n = strs.length;
        int[] parent = new int[n];
        for (int i = 0; i < n; i++)
            parent[i] = i;

        for (int i = 0; i < n; i++) {
    
    
            for (int j = i + 1; j < n; j++)
                if (find(parent, i) != find(parent, j))
                    if (check(strs[i], strs[j]))
                        union(parent, i, j);
        }

        int ans = 0;
        for (int i = 0; i < n; i++)
            if (parent[i] == i)
                ans++;
        return ans;       
    }

    public boolean check(String str1, String str2) {
    
    
        int diff = 0;//字符串都是字母异位词,只需判断各位不同字母的个数
        for (int i = 0; i < str1.length(); i++)
            if (str1.charAt(i) != str2.charAt(i)) {
    
    
                diff++;
                if (diff > 2)
                    return false;
            }
        return true;
    }

    public int find(int[] parent, int i) {
    
    
        if (parent[i] != i)
            parent[i] = find(parent, parent[i]);
        return parent[i];
    }

    public void union(int[] parent, int s1, int s2) {
    
    
        parent[find(parent, s2)] = find(parent, s1);
    }
}

(3) Results

Execution time: 13 ms, beating 95.14% of users
in all Java submissions ; memory consumption: 38.2 MB, beating 58.87% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/113797870