Loading content...
In the world of cryptography and pattern recognition, cyclic character equivalence is a fundamental concept. Two strings are considered cyclically equivalent if one can be transformed into the other through a series of uniform character shifts across the alphabet.
Shift Operations:
Forward Shift: Replace every character in the string with its successor in the English alphabet, where 'z' wraps around to 'a'. For example, "abc" can be forward-shifted to "bcd", and "xyz" becomes "yza".
Backward Shift: Replace every character in the string with its predecessor in the English alphabet, where 'a' wraps around to 'z'. For example, "bcd" can be backward-shifted to "abc", and "yza" becomes "xyz".
By applying these shift operations repeatedly (in either direction), any string can generate an infinite cyclic equivalence sequence.
For example, the string "abc" belongs to the following cyclic sequence:
... ↔ "zab" ↔ "abc" ↔ "bcd" ↔ "cde" ↔ ... ↔ "xyz" ↔ "yza" ↔ "zab" ↔ ...
Given an array of strings, your task is to group all strings that belong to the same cyclic equivalence sequence. The order of groups in the output and the order of strings within each group does not matter.
strings = ["abc","bcd","acef","xyz","az","ba","a","z"][["a","z"],["az","ba"],["abc","bcd","xyz"],["acef"]]The strings can be grouped by their cyclic equivalence relationships: • "a" and "z" are equivalent because 'a' can be shifted forward 25 times (or backward 1 time) to become 'z'. • "az" and "ba" are equivalent because shifting "az" forward by 1 yields "ba" (a→b, z→a). • "abc", "bcd", and "xyz" are all equivalent — they share the same relative character differences (each consecutive character differs by 1). • "acef" stands alone as it has a unique pattern (differences: +2, +2, +1) that no other string in the input matches.
strings = ["a"][["a"]]With only one string in the input, it forms its own group.
strings = ["a","b","c","d"][["a","b","c","d"]]All single-character strings are cyclically equivalent to each other, since any single character can be shifted to become any other single character. Thus, "a", "b", "c", and "d" all belong to the same equivalence group.
Constraints