Skip to the code
cf.
+25 XP

#35 Group Shifted Icon Codes

medium

An icon font names its glyphs with short lowercase codes. Two codes are "shifted" versions of each other if they have the same length and you can turn one into the other by moving every letter forward the same number of places in the alphabet, wrapping from z back to a. For example, "ace" shifted by 1 is "bdf", and "yza" shifted by 2 is "abc".

Write groupShifted(codes) that groups codes that are shifted versions of each other. Return the groups in the order their first code appears, with codes inside a group in their original order.

Example 1

Input: codes = ["ace","bdf","yac","ab","za","q","hello"]
Output: [["ace","bdf","yac"],["ab","za"],["q"],["hello"]]

In ace, bdf, and yac, each letter is 2 places after the one before it (wrapping for yac). ab and za both step by 1.

Example 2

Input: codes = ["abc"]
Output: [["abc"]]

One code is a group of one.

Constraints

  • 0 <= codes.length <= 10^4
  • 0 <= codes[i].length <= 50
  • Codes use lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).