Practice/Meta/Leetcode 744. Find Smallest Letter Greater Than Target
CodingMust
You are given a sorted array of lowercase English letters and a target character. Find the smallest character in the array that is strictly greater than the target character.
The array is considered cyclic, meaning if no character is greater than the target, you should return the first character in the array (wrapping around).
The array will always contain at least two characters, and characters may repeat.
2 <= letters.length <= 10^4letters are lowercase English lettersletters is sorted in non-decreasing orderletters contains at least two different characterstarget is a lowercase English letterExample 1:
Input: letters = ['c', 'f', 'j'], target = 'a' Output: 'c' Explanation: The smallest character that is greater than 'a' is 'c'.
Example 2:
Input: letters = ['c', 'f', 'j'], target = 'c' Output: 'f' Explanation: We need strictly greater, so 'c' doesn't count. The answer is 'f'.
Example 3:
Input: letters = ['c', 'f', 'j'], target = 'k' Output: 'c' Explanation: No character is greater than 'k', so we wrap around to 'c'.
Example 4:
Input: letters = ['a', 'b', 'b', 'b', 'c', 'e'], target = 'b' Output: 'c' Explanation: Even though 'b' appears multiple times, we need strictly greater, so 'c' is the answer.