# 744. Find Smallest Letter Greater Than Target

### Approach 1: Binary Search using `bisect`

```python
class Solution(object):
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """
        index = bisect.bisect(letters, target)
        return letters[index % len(letters)]
```

As the documentation of bisect reads, bisect.bisect returns an insertion point which comes after (to the right of) any existing entries of target in letters

> `bisect.bisect`(*a*, *x*, *lo=0*, *hi=len(a)*)
>
> > Similar to [`bisect_left()`](https://docs.python.org/3.6/library/bisect.html#bisect.bisect_left), but returns an insertion point which comes after (to the right of) any existing entries of *x* in *a*.
> >
> > The returned insertion point *i* partitions the array *a* into two halves so that `all(val <= x for valin a[lo:i])` for the left side and `all(val > x for val in a[i:hi])` for the right side.

This algorithm comes in at 100% percentile runtime

> **Time Complexity:** *O(logN),* because it uses a basic bisection
>
> **Space Complexity:** *O(1)*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programming.arora-aditya.com/leetcode/binary-search/744.-find-smallest-letter-greater-than-target.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
