#744

Find Smallest Letter Greater Than Target

pupil · 425 · lc easy +25 · verified · 58.9% accepted · 5,131 likes · top 56%

Description

You are given a sorted (non-decreasing) character array letters with at least two distinct characters, and a character target. Return the smallest character in letters that is strictly greater than target. If no such character exists, wrap around and return the first character in letters.

Example 1:

Input: letters = ["c","f","j"], target = "a"
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.

Example 2:

Input: letters = ["c","f","j"], target = "c"
Output: "f"
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.

Example 3:

Input: letters = ["x","x","y","y"], target = "z"
Output: "x"
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].

Code

1
2
3