Find Smallest Letter Greater Than Target
1 approach, code in every language
Problem description
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 letter
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Next Divider Label Round the Drawer is our own problem on the technique behind Find Smallest Letter Greater Than Target: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free