Group Shift-Equivalent Strings
Problem statement
Two lowercase strings are shift-equivalent when they have the same length and one can be obtained from the other by shifting every letter forward by the same amount in the cyclic alphabet. Shifts wrap from z to a.
Equivalently, the modulo-26 distance between each adjacent pair of letters is the same in both strings. For example, "aaa" and "zzz" are equivalent, and "abc" and "bcd" are equivalent.
Given strings, return all shift-equivalent groups. Preserve input order within each group, and order the groups by the first input position belonging to each group. Preserve duplicate strings as separate entries.
Function
groupShiftEquivalentStrings(strings: String[]) → String[][]Examples
Example 1
strings = ["aaa","zzz","abc","bcd"]return = [["aaa","zzz"],["abc","bcd"]]The first pair has only zero adjacent distances. The second pair has adjacent distance 1 at every position.
Example 2
strings = ["abc","bcd","acef","xyz","az","ba","a","z"]return = [["abc","bcd","xyz"],["acef"],["az","ba"],["a","z"]]Each group shares one modulo-distance signature. All one-letter strings share the empty signature.
Example 3
strings = ["a","a","b","aa","bb"]return = [["a","a","b"],["aa","bb"]]Duplicates remain present, all one-letter strings are equivalent, and both two-letter strings have distance zero.
Constraints
1 <= strings.length <= 100000.1 <= strings[i].length <= 100000.- Every string contains only lowercase English letters.
- The total number of characters is at most
100000.