A rotary combination lock has a ring of notches engraved with lowercase letters. Reading clockwise starting from a fixed reference point, the engraved letters appear in non-decreasing alphabetical order (the same letter may be engraved on more than one notch). A locksmith rests a pointer on a target letter — which does not have to match any notch on the ring — and needs to know which engraved letter sits on the very next notch strictly past it when turning clockwise. Because the ring is circular, if no engraved letter is alphabetically greater than the target, the search wraps around and the answer is the very first engraved letter on the ring.
notches of lowercase English letters (2 <= length <= 10000), sorted in non-decreasing order, representing the letters engraved on the ring read clockwise from the reference point.target.Print the single letter engraved on the next notch strictly clockwise past target, wrapping around to the first letter of notches if no engraved letter is greater than target.
Example 1
Input
cfj a
Expected
c
Explanation
The notches are c, f, j. The target a is alphabetically before all of them, so the next notch clockwise past a is the first engraved letter greater than a, which is c.
Example 2
Input
cfj c
Expected
f
Explanation
The target c matches an engraved notch exactly, but we need the next notch strictly greater than c. Skipping past c itself, the next engraved letter is f.
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 →