LeetCode 839: Similar String Groups Solution

Master LeetCode problem 839 (Similar String Groups), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation

To solve this problem, we can use the concept of disjoint sets (Union-Find). We can iterate through all pairs of strings and if they are similar, we union their respective sets. After processing all pairs, the number of disjoint sets remaining will give us the number of groups.

For checking similarity between two strings, we can compare them character by character and keep track of the positions where the characters differ. If the number of differing positions is at most 2, the strings are similar.

  • Time complexity: O(n^2 * m) where n is the number of strings and m is the length of each string.
  • Space complexity: O(n)

Solution Code

class Solution {
    public int numSimilarGroups(String[] strs) {
        int n = strs.length;
        DSU dsu = new DSU(n);

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (areSimilar(strs[i], strs[j])) {
                    dsu.union(i, j);
                }
            }
        }

        return dsu.getGroups();
    }

    private boolean areSimilar(String a, String b) {
        int diffCount = 0;
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) != b.charAt(i)) {
                diffCount++;
                if (diffCount > 2) {
                    return false;
                }
            }
        }
        return true;
    }

    class DSU {
        int[] parent;

        public DSU(int n) {
            parent = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
            }
        }

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

        public void union(int x, int y) {
            parent[find(x)] = find(y);
        }

        public int getGroups() {
            int groups = 0;
            for (int i = 0; i < parent.length; i++) {
                if (parent[i] == i) {
                    groups++;
                }
            }
            return groups;
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 839 (Similar String Groups)?

This page provides optimized solutions for LeetCode problem 839 (Similar String Groups) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 839 (Similar String Groups)?

The time complexity for LeetCode 839 (Similar String Groups) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 839 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 839 in Java, C++, or Python.

Back to LeetCode Solutions