words where every word has the same length and uses the same multiset of letters (each word is a rearrangement of any other). The words fall into **families**: two words share a family if you can get from one to the other through a chain of words from the list in which every neighboring pair is similar. For example, "abcd" and "badc" are not similar, but they end up in one family if the list also contains "bacd".
Return the number of families.words=['stone', 'tones', 'notes', 'onset', 'tonse'] → 3
"tones" is similar to "notes" (swap t and n) and to "tonse" (swap e and s), so these three words form one family. "stone" and "onset" are similar to nothing else in the list, giving two more families. That makes 3.
words=['abcd', 'bacd', 'badc', 'cdab'] → 2
"abcd" → "bacd" → "badc" are linked by a chain of swaps, even though "abcd" and "badc" are not directly similar. "cdab" differs from each of them in all four positions. The answer is 2.
words=['xy', 'xy'] → 1
Identical words count as similar, so there is one family.
len(words) ≤ 300len(words[i]) ≤ 300words[i] consists of lowercase English letterswords=['stone', 'tones', 'notes', 'onset', 'tonse']
3
Run checks the first 3 tests in your browser. Submit checks all 30 tests on the server, hidden ones included.