AlgoMaster Logo

Find Smallest Letter Greater Than Target

easy5 min readUpdated June 23, 2026

Understanding the Problem

We have a sorted array of lowercase letters and a target character. The task is to find the smallest letter in the array that comes strictly after the target alphabetically. Equal does not count, the letter must be strictly greater. If the target is greater than or equal to every letter in the array, we wrap around and return the first letter.

The wrap-around makes the alphabet behave like a circle. If no letter sits past the target, the answer loops back to index 0.

The array is sorted, which is what makes binary search applicable. We start with a linear scan to establish the baseline, then improve on it.

Key Constraints:

  • letters is sorted in non-decreasing order → This is the constraint that drives the approach. A sorted array supports binary search.
  • letters[i] is a lowercase English letter → Only 26 distinct values are possible, but the algorithm does not depend on that small alphabet.
  • target is a lowercase English letter → Input is always valid, so no validation is needed.

Approach 1: Linear Scan

Intuition

Walk through the array from left to right and return the first character that is strictly greater than the target. Because the array is sorted in non-decreasing order, the first such character encountered is also the smallest one that qualifies.

If the loop reaches the end without finding any greater character, the target is greater than or equal to every letter. The problem then asks us to wrap around and return the first letter.

Algorithm

  1. Iterate through each character in letters from left to right.
  2. If the current character is strictly greater than target, return it immediately.
  3. If the loop finishes without finding any character greater than target, return letters[0].

Example Walkthrough

1Start scan: target = 'c', check letters[0]
0
c
i
1
f
2
j
1/3

Code

The linear scan ignores the sorted structure of the array. Binary search uses that structure to find the answer in O(log n) instead of O(n).

Approach 2: Binary Search

Intuition

We are not searching for an exact match. We want the smallest element that is strictly greater than the target, which is the "find the leftmost position satisfying a condition" variant of binary search.

At each step we look at the middle element. If letters[mid] > target, then mid is a valid answer, but a smaller valid one might exist to the left, so we record mid as a candidate and search the left half. If letters[mid] <= target, then mid and everything to its left are too small or equal, so we search the right half.

The wrap-around is handled by initializing result = 0 before the loop. If no letter is greater than the target, result never changes and we return the first element, with no special case at the end.

Algorithm

  1. Initialize left = 0, right = letters.length - 1, and result = 0 (default to index 0 for wrap-around).
  2. While left <= right:
    • Compute mid = left + (right - left) / 2.
    • If letters[mid] > target, save result = mid and search left: set right = mid - 1.
    • Otherwise (letters[mid] <= target), search right: set left = mid + 1.
  3. Return letters[result].

Example Walkthrough

1Initialize: left=0, right=2, result=0, target='c'
0
result
c
left
1
f
2
j
right
1/4

Code