Find Smallest Letter Greater Than Target - Problem
You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
Input & Output
Example 1 — Basic Case
$
Input:
letters = ["c","f","j"], target = "a"
›
Output:
c
💡 Note:
The smallest character greater than 'a' is 'c'
Example 2 — Target in Middle
$
Input:
letters = ["c","f","j"], target = "c"
›
Output:
f
💡 Note:
The smallest character greater than 'c' is 'f'
Example 3 — Wrap Around
$
Input:
letters = ["c","f","j"], target = "z"
›
Output:
c
💡 Note:
No character greater than 'z', so return first character 'c'
Constraints
- 2 ≤ letters.length ≤ 104
- letters[i] is a lowercase English letter
- letters is sorted in non-decreasing order
- letters contains at least two different characters
- target is a lowercase English letter
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code