745. Find Smallest Letter Greater Than Target
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the smallest character in a sorted array of characters letters that is lexicographically greater than a given target character. A crucial detail is the "wrap-around" behavior: if no character in the array is greater than the target, we should return the very first character of the array.
Solution
Find Smallest Letter Greater Than Target in Python
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
n = len(letters)
low, high = 0, n
# Binary search to find the first character strictly greater than the target.
# This is equivalent to finding the 'upper_bound' of the target, or bisect_right in Python.
while low < high:
mid = low + (high - low) // 2
if letters[mid] > target:
# The character at mid is a potential answer.
# We try to find a smaller character that is also greater than target,
# so we search in the left half, including mid.
high = mid
else:
# The character at mid is not greater than target,
# so the answer must be in the right half.
low = mid + 1
# After the loop, 'low' is the index of the smallest character > target.
# If no such character exists, 'low' will be equal to 'n'.
# The modulo operator elegantly handles the wrap-around case.
# If low is n, low % n will be 0, returning the first character.
# Otherwise, it returns letters[low].
return letters[low % n]
Find Smallest Letter Greater Than Target in Java
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
int n = letters.length;
int low = 0, high = n;
// Binary search to find the first character strictly greater than the target.
// This is equivalent to finding the 'upper_bound' of the target.
while (low < high) {
int mid = low + (high - low) / 2;
if (letters[mid] > target) {
// The character at mid is a potential answer.
// We try to find a smaller character that is also greater than target,
// so we search in the left half, including mid.
high = mid;
} else {
// The character at mid is not greater than target,
// so the answer must be in the right half.
low = mid + 1;
}
}
// After the loop, 'low' is the index of the smallest character > target.
// If no such character exists, 'low' will be equal to 'n'.
// The modulo operator elegantly handles the wrap-around case.
// If low is n, low % n will be 0, returning the first character.
// Otherwise, it returns letters[low].
return letters[low % n];
}
}
Find Smallest Letter Greater Than Target in C++
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
int n = letters.size();
int low = 0, high = n;
// Binary search to find the first character strictly greater than the target.
// This is equivalent to finding the 'upper_bound' of the target.
while (low < high) {
int mid = low + (high - low) / 2;
if (letters[mid] > target) {
// The character at mid is a potential answer.
// We try to find a smaller character that is also greater than target,
// so we search in the left half, including mid.
high = mid;
} else {
// The character at mid is not greater than target,
// so the answer must be in the right half.
low = mid + 1;
}
}
// After the loop, 'low' is the index of the smallest character > target.
// If no such character exists, 'low' will be equal to 'n'.
// The modulo operator elegantly handles the wrap-around case.
// If low is n, low % n will be 0, returning the first character.
// Otherwise, it returns letters[low].
return letters[low % n];
}
};
Find Smallest Letter Greater Than Target in TypeScript
function nextGreatestLetter(letters: string[], target: string): string {
const n = letters.length;
let low = 0, high = n;
// Binary search to find the first character strictly greater than the target.
// This is equivalent to finding the 'upper_bound' of the target.
while (low < high) {
// Use Math.floor for integer division in TypeScript
const mid = low + Math.floor((high - low) / 2);
if (letters[mid] > target) {
// The character at mid is a potential answer.
// We try to find a smaller character that is also greater than target,
// so we search in the left half, including mid.
high = mid;
} else {
// The character at mid is not greater than target,
// so the answer must be in the right half.
low = mid + 1;
}
}
// After the loop, 'low' is the index of the smallest character > target.
// If no such character exists, 'low' will be equal to 'n'.
// The modulo operator elegantly handles the wrap-around case.
// If low is n, low % n will be 0, returning the first character.
// Otherwise, it returns letters[low].
return letters[low % n];
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.