Skip to main content
Back to problems
Leetcode
Medium
Arrays
Binary Search
Ordered Structures
Find Smallest Letter Greater Than Target

Find the smallest letter in a sorted circular list that is strictly greater than a given target letter.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

You are given a list of lowercase letters sorted in non-decreasing order. The list behaves like a circle, so after the last letter, the search wraps back to the first letter.

Given a target letter, return the smallest letter in the list that is strictly greater than the target. If no such letter exists, wrap around and return the first letter in the list.

The answer is guaranteed to exist because the list is non-empty and circular.

Input Format

  • An integer array-like list of lowercase letters sorted in non-decreasing order.
  • A single target lowercase letter.

Example representation:

clike

letters = ['c','f','j'], target = 'a'

Output Format

  • Return a single lowercase letter: the smallest element in letters that is strictly greater than target, or the first letter if the search wraps around.

Constraints

  • 1 <= letters.length <= $10^{4}$
  • letters is sorted in non-decreasing order
  • letters[i] and target are lowercase English letters
  • The answer is guaranteed to exist
Examples
Sample cases returned by the problem API.

Example 1

Input

letters = ['c','f','j'], target = 'a'

Output

'c'

Explanation

'c' is the first letter greater than 'a'.

Example 2

Input

letters = ['c','f','j'], target = 'c'

Output

'f'

Explanation

The answer must be strictly greater than the target, so skip 'c' and return 'f'.

Show 1 more example

Example 3

Input

letters = ['c','f','j'], target = 'k'

Output

'c'

Explanation

No letter is greater than 'k', so the search wraps around to the first letter.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.