EasyArrayBinary Search

1 approach, code in every language

Problem description

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

After the pattern

Now solve one you haven't seen.

Next Divider Label Round the Drawer is our own problem on the technique behind Find Smallest Letter Greater Than Target: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free