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.
Input Format
- An integer array-like list of lowercase letters sorted in non-decreasing order.
- A single target lowercase letter.
Example representation:
clike
letters = ['c','f','j'], target = 'a'Output Format
- Return a single lowercase letter: the smallest element in
lettersthat is strictly greater thantarget, or the first letter if the search wraps around.
Constraints
1 <= letters.length <= $10^{4}$lettersis sorted in non-decreasing orderletters[i]andtargetare lowercase English letters- The answer is guaranteed to exist
Example 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'.
Show 1 more example
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
Unlock deeper context for this problem
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.