Groups of Strings - Problem
String Grouping Challenge: You are given an array of strings where each string contains only unique lowercase English letters. Your task is to group these strings based on their connectivity.

Two strings are connected if you can transform one into the other using exactly one of these operations:
Add exactly one letter
Delete exactly one letter
Replace exactly one letter

Strings belong to the same group if they are connected directly or through a chain of connections. Your goal is to find:
1. The maximum number of groups possible
2. The size of the largest group

Example: ["a", "ab", "abc"] forms one group since "a" → "ab" (add 'b') → "abc" (add 'c')

Input & Output

example_1.py — Basic Connection Chain
$ Input: words = ["a", "b", "ab", "cde"]
Output: [3, 2]
💡 Note: Group 1: {"a", "ab"} - connected by adding 'b'. Group 2: {"b", "ab"} - connected by adding 'a'. Wait, "ab" connects both "a" and "b", so Group 1: {"a", "ab", "b"} of size 3. Group 2: {"cde"} of size 1. Total: 2 groups, max size 3.
example_2.py — Replace Operations
$ Input: words = ["abc", "abd", "xyz"]
Output: [2, 2]
💡 Note: "abc" and "abd" are connected by replacing 'c' with 'd'. "xyz" is isolated. Result: 2 groups, largest has size 2.
example_3.py — Single Characters
$ Input: words = ["a", "b", "c", "ab"]
Output: [2, 3]
💡 Note: "ab" can connect to "a" (delete 'b') and "b" (delete 'a'). Through "ab", all single chars connect. "c" connects to "ab" by replacing. One big group of size 4? No wait - "c" and "ab" need replace 'c'→'a' AND add 'b', that's 2 ops. Groups: {"a","ab","b"} and {"c"}. Result: 2 groups, max size 3.

Visualization

Tap to expand
String Groups Visualization"a"000001"ab"000011"abc"000111Group 1: Size 3"xyz"111000Group 2: Size 1Connection Rules (Bitmask Perspective)• Add char: mask2 = mask1 | (1 << newChar)• Delete char: mask2 = mask1 ^ (1 << delChar)• Replace char: mask2 = (mask1 ^ (1 << oldChar)) | (1 << newChar)Final Answer: [2, 3]2 groups totalLargest group has 3 strings
Understanding the Visualization
1
Convert to Bitmasks
Each string becomes a unique bitmask pattern
2
Find Connections
Two bitmasks are connected if they differ by 1-2 bits in specific patterns
3
Union Components
Merge connected strings into groups using Union-Find
4
Count Results
Return number of groups and size of largest group
Key Takeaway
🎯 Key Insight: Bitmasks make connection checking extremely fast - just bitwise operations instead of character-by-character comparisons!

Time & Space Complexity

Time Complexity
⏱️
O(n × 26²)

For each string, check at most 26² possible connections (add/delete 26 chars, replace 26×26)

n
2n
Linear Growth
Space Complexity
O(n)

HashMap for bitmask mapping and Union-Find parent array

n
2n
Linearithmic Space

Constraints

  • 1 ≤ words.length ≤ 2 × 104
  • 1 ≤ words[i].length ≤ 26
  • words[i] consists of lowercase English letters only
  • No letter occurs more than once in any string
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
28.4K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen