Ctrl K

Letter Set Clusters

Hard
You are given an array of strings words. Each string consists of lowercase English letters with no letter repeated, so each one can be treated as a set of letters. Two strings are linked if the letter set of one can be turned into the letter set of the other with exactly one move: - add one letter; - remove one letter; - replace one letter with any letter, including itself. Links join the strings into clusters: two strings are in the same cluster when a chain of links leads from one to the other. Every array element counts separately, duplicates included. Return [number of clusters, size of the largest cluster].
Example 1
words=['ab', 'abc', 'xy', 'xz', 'q'] → [3, 2]

"ab" → "abc" by adding a letter, "xy" → "xz" by swapping y for z, and "q" links to nothing. Three groups, the largest has two words.

Example 2
words=['ba', 'ab', 'cd'] → [2, 2]

"ba" and "ab" are the same letter set (replace a letter with itself), while "cd" differs from them in two letters.

Example 3
words=['k', 'm', 'mn', 'mno', 'pq'] → [2, 4]

"k" → "m" by replacement, "m" → "mn" → "mno" by adding letters: a group of four; "pq" is on its own.

Constraints
  • 1 ≤ len(words) ≤ 2·10⁴
  • 1 ≤ len(words[i]) ≤ 26
  • words[i] consists of distinct lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Unambiguous Shorthand HardOne-Swap Word Families HardAssembling Fragments HardWords That Fit Each Puzzle HardBest Word Set From Tiles Hard
loading Python…
Input
words=['ab', 'abc', 'xy', 'xz', 'q']
Expected
[3, 2]

Run checks the first 3 tests in your browser. Submit checks all 66 tests on the server, hidden ones included.