Group Rotation-Equivalent Strings
Problem statement
Given a list of strings, group strings that can be converted into each other by rotating characters. A rotation moves any prefix to the end, preserving the character order.
For this exercise, assume strings contain lowercase ASCII letters and may be empty. Return all groups in the order their first member appears in the input. Within each group, keep the original input order and retain duplicate entries. Empty strings are rotation-equivalent to each other. Rotation never changes string length; anagrams with a different cyclic order belong to different groups.
Function
groupRotations(words: String[]) → String[][]Examples
Example 1
words = ["abc","bca","cab","xyz"]return = [["abc","bca","cab"],["xyz"]]The source example groups abc, bca and cab; xyz forms a separate group.
Example 2
words = ["xyz","cab","zxy","abc","bca"]return = [["xyz","zxy"],["cab","abc","bca"]]The xyz group appears first because its first member occurs first. Every group keeps input member order.
Example 3
words = ["","","a","a","aa"]return = [["",""],["a","a"],["aa"]]Empty strings and repeated entries are preserved; a and aa are not rotations of each other.
Constraints
- For this exercise, assume
0 <= words.length <= 200. - Each string has at most
100lowercase ASCII letters. - The sum of string lengths is at most
2000.