EasyArrayBinary Search

Find Smallest Letter Greater Than Target

LeetCode
1 approach, code in all languages

You are handed a list of lowercase letters already sorted in non-decreasing order, plus a single target letter. Return the smallest letter in the list that comes strictly after the target in the alphabet.

The list is treated as circular: if no letter beats the target, the search wraps around and the answer becomes the first letter of the list.

Example 1

Input: letters = ["c","f","j"], target = "a"

Output: "c"

The smallest letter after 'a' present in the list is 'c'.

Example 2

Input: letters = ["c","f","j"], target = "j"

Output: "c"

Nothing beats 'j', so the search wraps and returns the first letter 'c'.

Constraints

  • 2 ≤ letters.length ≤ 10^4
  • letters[i] is a lowercase English letter
  • letters is sorted in non-decreasing order
  • target is a lowercase English letter
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now