249. Group Shifted Strings
Problem
Group strings that belong to the same shifting sequence (can be shifted to form each other).
A shifting sequence is formed by shifting each character in a string by the same amount. For example, "abc" shifted by 1 becomes "bcd", shifted by 2 becomes "cde".
Given an array of strings, group all strings that belong to the same shifting sequence together. Return a list of groups, where each group is a list of strings.
Examples
Input: ["abc","bcd","acef","xyz","az","ba","a","z"]
Output: [["acef"],["a","z"],["abc","bcd","xyz"],["az","ba"]]
Input: ["a"]
Output: [["a"]]
Hints
Compute a "normalized" key for each string by converting each character to its offset from the first character.
Use a hash map with this normalized key to group strings.
Handle empty strings as a special case.
Related Problems
249. Group Shifted Strings
Group strings that belong to the same shifting sequence (can be shifted to form each other).