words of equal-length strings made of lowercase English letters.
In one move you may swap two characters of a string that sit at positions of the same parity: both even or both odd (0-based). You can make as many moves as you like.
Call two strings related if one can be turned into the other with such moves. Being related splits the list into classes: strings within a class are related, and strings from different classes are not.
Return the number of classes.words=["stop", "pots", "tops", "opts"] → 3
The even positions of "stop" hold s, o and the odd ones hold t, p. In "pots" the even positions hold p, t, a different set. The classes are {stop}, {pots, tops}, {opts}: "pots" turns into "tops" by swapping p and t at positions 0 and 2.
words=["ab", "ba", "ab"] → 2
In a two-letter word the letters sit at positions of different parity, so they cannot be swapped. The classes are {ab, ab} and {ba}.
words=["abcde", "edcba", "cbade"] → 1
words=["stop", "pots", "tops", "opts"]
3
Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.