422. Find Smallest Letter Greater Than Target

EasyBinary SearchArrayBinary Search

Given a sorted array of characters letters and a character target, return the smallest character in letters that is strictly greater than target. Letters wrap around: if no character is greater, return letters[0].

Input: A sorted character array letters and a character target.

Output: The smallest character greater than target (wraps to letters[0] if none).

Examples

Example 1
Input: ['c','f','j'], 'a'
Output: 'c'
Explanation: 'c' is the smallest letter greater than 'a'.
Example 2
Input: ['c','f','j'], 'c'
Output: 'f'
Explanation: 'c' is not strictly greater than 'c'; next is 'f'.
Example 3
Input: ['c','f','j'], 'j'
Output: 'c'
Explanation: No letter greater than 'j'; wrap around to letters[0]='c'.

Constraints

Asked by

GoogleBloombergAmazonMetaMicrosoft
Solve this problem in the editor →