744. Find Smallest Letter Greater Than Target LeetCode Solution
In this guide, you will get 744. Find Smallest Letter Greater Than Target LeetCode Solution with the best time and space complexity. The solution to Find Smallest Letter Greater Than Target problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Smallest Letter Greater Than Target solution in C++
- Find Smallest Letter Greater Than Target solution in Java
- Find Smallest Letter Greater Than Target solution in Python
- Additional Resources

Problem Statement of Find Smallest Letter Greater Than Target
You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, 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].
Constraints:
2 <= letters.length <= 104
letters[i] is a lowercase English letter.
letters is sorted in non-decreasing order.
letters contains at least two different characters.
target is a lowercase English letter.
Complexity Analysis
- Time Complexity: O(\log n)
- Space Complexity: O(1)
744. Find Smallest Letter Greater Than Target LeetCode Solution in C++
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
int l = 0;
int r = letters.size();
while (l < r) {
const int m = (l + r) / 2;
if (letters[m] > target)
r = m;
else
l = m + 1;
}
return letters[l % letters.size()];
}
};
/* code provided by PROGIEZ */
744. Find Smallest Letter Greater Than Target LeetCode Solution in Java
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
int l = 0;
int r = letters.length;
while (l < r) {
final int m = (l + r) / 2;
if (letters[m] > target)
r = m;
else
l = m + 1;
}
return letters[l % letters.length];
}
}
// code provided by PROGIEZ
744. Find Smallest Letter Greater Than Target LeetCode Solution in Python
class Solution:
def nextGreatestLetter(self, letters: list[str], target: str) -> str:
l = bisect.bisect_right(range(len(letters)), target,
key=lambda m: letters[m])
return letters[l % len(letters)]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.