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:
•
•
•
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:
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 letterStrings 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
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)
✓ Linear Growth
Space Complexity
O(n)
HashMap for bitmask mapping and Union-Find parent array
⚡ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code