FunctionRacerProblemsRace

Shift Families

MediumHash mapsPython, JavaScript, TypeScript, Java, C++

Shifting a word moves every one of its letters forward by the same number of places in the alphabet, wrapping around from z to a: shifting "abc" by 1 gives "bcd", and shifting "xyz" by 3 gives "abc". Two words belong to the same family when one is a shift of the other; a shift by 0 counts, so equal words are in the same family, and words of different lengths never are.

Group the words of words into families and return them as a list of lists: each family sorted alphabetically (duplicates kept), and the families ordered by their first word. The empty list gives [].

Example 1

words ["abc","bcd","acef","xyz","az","ba","a","z"]
returns [["a","z"],["abc","bcd","xyz"],["acef"],["az","ba"]]

abc, bcd and xyz are shifts of each other; so are az and ba (shift by 1); a and z are one-letter words, which are always shifts of each other; acef is alone. Families are listed by their first word: a, abc, acef, az.

Example 2

words ["cat","cat","dog"]
returns [["cat","cat"],["dog"]]

The two copies of cat stay together; dog is not a shift of cat.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems