48. Find Smallest Letter Greater Than Target

EasyArrayArray

You are given a list of sorted characters letters and a target character target. Return the smallest character in the list that is strictly greater than target. The letters wrap around — if no letter is greater, return the first letter.

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

Output: The smallest character strictly greater than target (wraps around).

Examples

Example 1
Input: ['c','f','j'],'a'
Output: 'c'
Explanation: Smallest letter > 'a' is 'c'.
Example 2
Input: ['c','f','j'],'c'
Output: 'f'
Explanation: 'c' is not > 'c'. Next is 'f'.
Example 3
Input: ['c','f','j'],'j'
Output: 'c'
Explanation: No letter > 'j', wrap around to 'c'.

Constraints

Asked by

GoogleBloombergAmazonMetaMicrosoft
Solve this problem in the editor →