Find the smallest letter in a sorted circular list that is strictly greater than a given target letter.
You are given a list of lowercase letters sorted in non-decreasing order. The list behaves like a circle, so after the last letter, the search wraps back to the first letter.
Given a target letter, return the smallest letter in the list that is strictly greater than the target. If no such letter exists, wrap around and return the first letter in the list.
The answer is guaranteed to exist because the list is non-empty and circular.
Example representation:
clike
letters = ['c','f','j'], target = 'a'letters that is strictly greater than target, or the first letter if the search wraps around.1 <= letters.length <= $10^{4}$letters is sorted in non-decreasing orderletters[i] and target are lowercase English lettersExample 1
Input
letters = ['c','f','j'], target = 'a'
Output
'c'
Explanation
'c' is the first letter greater than 'a'.
Example 2
Input
letters = ['c','f','j'], target = 'c'
Output
'f'
Explanation
The answer must be strictly greater than the target, so skip 'c' and return 'f'.
Example 3
Input
letters = ['c','f','j'], target = 'k'
Output
'c'
Explanation
No letter is greater than 'k', so the search wraps around to the first letter.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.