Next Letter Up
A row of signs is labelled with letters, given as the string letters, sorted from a to z; a letter may appear more than once. Given a letter target, return the first letter in the row that comes after target in the alphabet.
The row wraps around: if no letter comes after target, the answer is the row's first letter.
Examples
letters = "cfj", target = "a" → "c"
letters = "cfj", target = "c" → "f"
letters = "cfj", target = "j" → "c"
letters = "eeegg", target = "e" → "g"In the third example nothing comes after j, so the row wraps around to c. In the last, the es equal the target and do not count.
Constraints
- Between 2 and 100,000 lowercase letters, sorted, with at least two different letters.
targetis one lowercase letter. - Aim for O(log n): a row four times longer should take only a couple of steps more.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.