Groups of Strings - Problem

You are given a 0-indexed array of strings words. Each string consists of lowercase English letters only. No letter occurs more than once in any string of words.

Two strings s1 and s2 are said to be connected if the set of letters of s2 can be obtained from the set of letters of s1 by any one of the following operations:

  • Adding exactly one letter to the set of the letters of s1.
  • Deleting exactly one letter from the set of the letters of s1.
  • Replacing exactly one letter from the set of the letters of s1 with any letter, including itself.

The array words can be divided into one or more non-intersecting groups. A string belongs to a group if any one of the following is true:

  • It is connected to at least one other string of the group.
  • It is the only string present in the group.

Note that the strings in words should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.

Return an array ans of size 2 where:

  • ans[0] is the maximum number of groups words can be divided into, and
  • ans[1] is the size of the largest group.

Input & Output

Example 1 — Basic Grouping
$ Input: words = ["a","b","ab","cde"]
Output: [3,2]
💡 Note: Groups: {"a","ab"} (connected by adding 'b'), {"b"} (isolated), {"cde"} (isolated). Total 3 groups, largest size 2.
Example 2 — Single Large Group
$ Input: words = ["a","ab","abc"]
Output: [1,3]
💡 Note: All strings are connected: "a" → "ab" (add 'b'), "ab" → "abc" (add 'c'). One group of size 3.
Example 3 — All Isolated
$ Input: words = ["abc","def","ghi"]
Output: [3,1]
💡 Note: No connections possible (differ by more than one character). 3 groups, each of size 1.

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 words[i]

Visualization

Tap to expand
Groups of Strings INPUT words array: "a" "b" "ab" "cde" Bitmask Encoding: "a" = 0...001 "b" = 0...010 "ab" = 0...011 "cde" = 0..11100 Connection Graph: a ab b cde ALGORITHM STEPS 1 Convert to Bitmasks Each char = bit position 2 Union-Find Init Each word = own group 3 Find Connections Add/Del/Replace 1 char 4 Union Groups Merge connected words Union-Find Process: "a" + 'b' = "ab" [OK] "b" + 'a' = "ab" [OK] "a" -- "cde" [X] "b" -- "cde" [X] Group1: {a, ab, b} Group2: {cde} FINAL RESULT Final Groups: Group 1 (size: 3) a ab b Group 2 (size: 1) cde Total: 2 groups formed Largest group size: 3 OUTPUT: [2, 3] [groups, max_size] Key Insight: Use bitmasks to represent each word (26 bits for letters). Two words are connected if their bitmasks differ by exactly 1 bit (add/delete) or XOR to a power of 2 (replace). Union-Find efficiently groups connected strings. Time: O(n * 26^2) for checking all possible 1-bit changes per mask. TutorialsPoint - Groups of Strings | Optimal Solution (Union-Find + Bitmask)
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
12.5K Views
Medium Frequency
~35 min Avg. Time
234 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