pyassistant

Group Rotationally Equivalent Strings by Indices

Given a list of strings words, group the indices of words that are rotations of each other. Two strings a and b are rotationally equivalent if b can be obtained by rotating a (cyclic shift). Return a list of groups, where each group is a list of 0-based indices of words that are rotations of one another. Each group should have its indices sorted in ascending order. The list of groups should be sorted by the smallest index in each group (i.e., by groups' first elements). Strings of different lengths cannot be rotations of each other and should not be grouped together. All words should appear in exactly one group (singletons are allowed when a word has no rotational matches).

Example:

Input:
(["abc", "bca", "cab", "abcd", "dabc", "xyz"],)
Output:
[[0, 1, 2], [3, 4], [5]]

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.