A letterpress compositor's tray holds 26 wooden type-slugs, one for each lowercase English letter, arranged in a fixed grid. Reading the alphabet in order, slugs fill five per row for five full rows — row 0 holds a b c d e, row 1 holds f g h i j, row 2 holds k l m n o, row 3 holds p q r s t, row 4 holds u v w x y — and the 26th slug, z, sits alone in row 5, in the leftmost column, directly under u. Formally, letter c (with p = ord(c) - ord('a'), 0 <= p <= 25) sits at row p // 5, column p % 5, except that z (p = 25) sits at row 5, column 0 instead of row 5, column 0 being derived by the formula (which would put it out of bounds) — z is simply the sole occupant of row 5, column 0.
The compositor's hand starts resting on the a slug (row 0, column 0). To typeset a word, the hand must visit the slug for each of the word's letters, in order, moving one grid cell at a time, and must never rest on a grid cell that has no slug (in particular, it can only be in row 5 while in column 0). After arriving at a letter's slug, the hand performs a pick action before moving toward the next letter.
Multiple equally-short routes between two slugs can exist, so the tray's automated arm follows one fixed rule to stay predictable: when moving from the slug it is currently resting on toward the next target slug, if the current slug is z, the arm performs all of its vertical moves first and then all of its horizontal moves; for every other current slug, it performs all of its horizontal moves first and then all of its vertical moves. (This rule is exactly what keeps the arm from ever passing through row 5 at a column other than 0.)
Encode a single vertical or horizontal step as one of the characters U (row - 1), D (row + 1), L (column - 1), R (column + 1), and encode a pick action as the character S. Starting from a, output the full character sequence of moves and picks needed to typeset the entire given word.
A single line containing a non-empty string word of lowercase English letters.
Print, on a single line, the move/pick character sequence described above — the concatenation, over the letters of word in order, of the moves needed to travel from the previous slug (starting from a) to that letter's slug (following the ordering rule above), followed by a single S.
1 <= length of word <= 100. word consists only of lowercase English letters ('a'-'z').
Example 1
Input
cab
Expected
RRSLLSRS
Explanation
Start at 'a' (row0,col0). To reach 'c' (row0,col2): current slug is not 'z', so horizontal moves first: 2 rights, 0 vertical -> "RR", then pick -> "RRS". Now at 'c' (row0,col2); to reach 'a' (row0,col0): horizontal first, 2 lefts -> "LL", pick -> "LLS". Now at 'a'; to reach 'b' (row0,col1): 1 right -> "R", pick -> "RS". Concatenating: "RRS" + "LLS" + "RS" = "RRSLLSRS".
Example 2
Input
zc
Expected
DDDDDSUUUUURRS
Explanation
Start at 'a' (row0,col0). To reach 'z' (row5,col0): current slug 'a' is not 'z', so horizontal first (0 moves, columns already match) then vertical: 5 downs -> "DDDDD", pick -> "DDDDDS". Now at 'z' (row5,col0); to reach 'c' (row0,col2): current slug IS 'z', so this time vertical moves come first: 5 ups -> "UUUUU", then horizontal: 2 rights -> "RR", pick -> "UUUUURRS". Concatenating: "DDDDDS" + "UUUUURRS" = "DDDDDSUUUUURRS".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →