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].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.
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.
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.
words=['ab', 'abc', 'xy', 'xz', 'q']
[3, 2]
Run checks the first 3 tests in your browser. Submit checks all 66 tests on the server, hidden ones included.