#2157

Groups of Strings

Hard
ArrayHash TableStringBit ManipulationUnion-FindUnion-FindGraph Traversal
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n * m²)
Space
O(1)
O(n)
💡

Intuition

Time O(n * m²)Space O(n)

We can use a Union-Find (Disjoint Set Union) structure to efficiently group connected strings. This allows us to quickly find and union groups without checking every pair explicitly.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize a Union-Find structure to manage groups.
  2. 2Step 2: For each string, compare it with all others to determine if they are connected, and union them if they are.
  3. 3Step 3: Count the number of unique groups and find the size of the largest group.
solution.py44 lines
1# Full working Python code
2class UnionFind:
3    def __init__(self, size):
4        self.parent = list(range(size))
5        self.rank = [1] * size
6
7    def find(self, x):
8        if self.parent[x] != x:
9            self.parent[x] = self.find(self.parent[x])
10        return self.parent[x]
11
12    def union(self, x, y):
13        rootX = self.find(x)
14        rootY = self.find(y)
15        if rootX != rootY:
16            if self.rank[rootX] > self.rank[rootY]:
17                self.parent[rootY] = rootX
18            elif self.rank[rootX] < self.rank[rootY]:
19                self.parent[rootX] = rootY
20            else:
21                self.parent[rootY] = rootX
22                self.rank[rootX] += 1
23
24def groups_of_strings(words):
25    n = len(words)
26    uf = UnionFind(n)
27
28    for i in range(n):
29        for j in range(i + 1, n):
30            if are_connected(words[i], words[j]):
31                uf.union(i, j)
32
33    group_count = len(set(uf.find(i) for i in range(n)))
34    group_sizes = [0] * n
35    for i in range(n):
36        group_sizes[uf.find(i)] += 1
37    max_group_size = max(group_sizes)
38    return [group_count, max_group_size]
39
40def are_connected(s1, s2):
41    set1, set2 = set(s1), set(s2)
42    if abs(len(set1) - len(set2)) > 1:
43        return False
44    return len(set1.symmetric_difference(set2)) <= 1

Complexity note: This complexity arises because each string can be compared with every other string, and the union-find operations are nearly constant time due to path compression.

  • 1Understanding how to determine connectivity between strings is crucial.
  • 2Using Union-Find can significantly optimize the grouping process.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.