You are handed a list of lowercase letters already sorted in non-decreasing order, plus a single target letter. Return the smallest letter in the list that comes strictly after the target in the alphabet.
The list is treated as circular: if no letter beats the target, the search wraps around and the answer becomes the first letter of the list.
Example 1
Input: letters = ["c","f","j"], target = "a"
Output: "c"
The smallest letter after 'a' present in the list is 'c'.
Example 2
Input: letters = ["c","f","j"], target = "j"
Output: "c"
Nothing beats 'j', so the search wraps and returns the first letter 'c'.
Constraints
2 ≤ letters.length ≤ 10^4letters[i] is a lowercase English letterletters is sorted in non-decreasing ordertarget is a lowercase English letterSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI