370. Group Shifted Strings
Perform the following shift operations on a string:
- Right shift: Replace every letter with the successive letter of the English alphabet, where 'z' is replaced by 'a'. For example, "abc" can be right-shifted to "bcd" or "xyz" can be right-shifted to "yza".
- Left shift: Replace every letter with the preceding letter of the English alphabet, where 'a' is replaced by 'z'. For example, "bcd" can be left-shifted to "abc" or "yza" can be left-shifted to "xyz".
We can keep shifting the string in both directions to form an endless shifting sequence.
- For example, shift "abc" to form the sequence: ... <-> "abc" <-> "bcd" <-> ... <-> "xyz" <-> "yza" <-> .... <-> "zab" <-> "abc" <-> ...
You are given an array of strings strings, group together all strings[i] that belong to the same shifting sequence. You may return the answer in any order.
Example 1:
Input:
strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
Grouping Process:
- "abc" -> "bcd" -> "xyz" (Same shifting sequence, difference pattern is (1,1))
- "acef" (No other word follows its shift pattern)
- "az" <-> "ba" (Shifts cyclically, difference pattern is (25))
- "a" and "z" (Single characters, always form their own group)
Output:
[["acef"], ["a", "z"], ["abc", "bcd", "xyz"], ["az", "ba"]]
Example 2:
Input:
strings = ["a"]
Grouping Process:
- Only one string "a", so it forms a group by itself.
Output:
[["a"]]
Now Your Turn!
Pick the correct output for the given inputInput :
strings = ["abc","aac"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= strings.length <= 200
- 1 <= strings[i].length <= 50
- strings[i] consists of lowercase English letters.