Group Shifted Strings

MediumArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Shifting a lowercase string moves every letter forward by one in the alphabet, with z wrapping around to a (so "abz" shifts to "bca"). Two strings belong to the same **shifting sequence** if one can be turned into the other by shifting it some number of times. Given an array strings, group together all strings that belong to the same shifting sequence. Return the groups as a list of lists with a fixed order: groups appear in the order of their first member's first appearance in strings, and inside each group the strings keep their original input order. Duplicate strings are kept, and each copy is placed in its group.

Examples

Input: strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
Output: [["abc","bcd","xyz"],["acef"],["az","ba"],["a","z"]]
`"az"` shifted 1 time becomes `"ba"`; every single-letter string is in one sequence. `"abc"` appears first, so its group comes first.
Input: strings = ["a"]
Output: [["a"]]
A single string forms its own group.

Constraints

  • 0 <= strings.length <= 200
  • 1 <= strings[i].length <= 50
  • `strings[i]` consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems