Group Shifted Strings - Problem

Given an array of strings strings, group together all strings that belong to the same shifting sequence.

A shifting sequence is formed by applying 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" → "bcd", "xyz" → "yza"
  • Left shift: Replace every letter with the preceding letter of the English alphabet, where 'a' is replaced by 'z'. For example, "bcd" → "abc", "yza" → "xyz"

Strings in the same shifting sequence can be transformed into each other through these operations. For example, "abc", "bcd", and "xyz" all belong to the same shifting sequence because:

"abc" → "bcd" → "cde" → ... → "xyz"

You may return the answer in any order.

Input & Output

Example 1 — Basic Grouping
$ Input: strings = ["abc","bcd","acf","xyz","az","ba","a","z"]
Output: [["abc","bcd","xyz"],["acf"],["az","ba"],["a"],["z"]]
💡 Note: "abc", "bcd", "xyz" have same pattern [1,1]. "az" and "ba" have same pattern [25]. Single characters form separate groups.
Example 2 — All Same Group
$ Input: strings = ["a","b","c"]
Output: [["a","b","c"]]
💡 Note: All single characters have empty pattern [], so they belong to same group.
Example 3 — Wraparound Pattern
$ Input: strings = ["za","yb"]
Output: [["za","yb"]]
💡 Note: "za" has pattern [1] (z→a = +1 with wraparound), "yb" has pattern [3] (y→b = +3 with wraparound). They have different patterns so belong to different groups.

Constraints

  • 1 ≤ strings.length ≤ 200
  • 1 ≤ strings[i].length ≤ 50
  • strings[i] consists of lowercase English letters only

Visualization

Tap to expand
Group Shifted Strings INPUT strings array: "abc" "bcd" "acf" "xyz" "az" "ba" "a" "z" Shifting Sequence Example: abc --> bcd --> xyz Each letter shifts by +1 (wrapping z --> a) Pattern Differences: "abc": b-a=1, c-b=1 --> "1,1" "bcd": c-b=1, d-c=1 --> "1,1" Same pattern = Same group! ALGORITHM STEPS 1 Create Hash Map Key: pattern, Value: strings 2 Compute Pattern Key Diff between adjacent chars 3 Group by Pattern Add string to map[pattern] 4 Return Groups Collect all map values Hash Map State: "1,1" --> [abc, bcd, xyz] "2,3" --> [acf] "25" --> [az, ba] "" --> [a] "" --> [z] (single chars have empty pattern) FINAL RESULT Grouped Strings: Group 1 (pattern "1,1"): ["abc", "bcd", "xyz"] Group 2 (pattern "2,3"): ["acf"] Group 3 (pattern "25"): ["az", "ba"] Group 4: ["a"] Group 5: ["z"] Output: [["abc","bcd","xyz"],["acf"], ["az","ba"],["a"],["z"]] OK - 5 Groups Key Insight: The pattern key is computed by calculating the difference between consecutive characters (mod 26 for wrapping). Strings with the same pattern belong to the same shifting sequence. Time: O(n*k), Space: O(n*k) where k = avg string length. Example: "az" has diff (z-a+26)%26 = 25, "ba" has diff (a-b+26)%26 = 25, so they group together! TutorialsPoint - Group Shifted Strings | Hash Map with Pattern Key Approach
Asked in
Google 25 Facebook 18 Amazon 12
28.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen