[LeetCode] Find Smallest Letter Greater Than Target

744. Find Smallest Letter Greater Than Target

Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.

Note that the letters wrap around.

  • For example, if target == ‘z’ and letters == [‘a’, ‘b’], the answer is ‘a’.
1
2
3
4
5
6
7
8
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
auto it = upper_bound(begin(letters), end(letters), target);
if(it == end(letters)) return letters[0];
return *it;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/04/PS/LeetCode/find-smallest-letter-greater-than-target/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.