> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Excel Sheet Column Title Python Solution

> Tested Python solution for LeetCode 168 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 168, Easy. Topics: Math, String. [View on LeetCode](https://leetcode.com/problems/excel-sheet-column-title/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 168   # by problem number
lcpy gen -s excel_sheet_column_title   # by problem name
```

## Problem

Given an integer `columnNumber`, return *its corresponding column title as it appears in an Excel sheet*.

For example:

```
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
```

### Examples

```
Input: columnNumber = 1
Output: "A"
```

```
Input: columnNumber = 28
Output: "AB"
```

```
Input: columnNumber = 701
Output: "ZY"
```

### Constraints

* 1 \<= columnNumber \<= 2^31 - 1

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/excel_sheet_column_title/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/excel_sheet_column_title/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log_26 n)
    # Space: O(log_26 n)
    def convert_to_title(self, column_number: int) -> str:
        result: list[str] = []

        while column_number > 0:
            column_number -= 1  # shift 1-indexed alphabet to 0-indexed
            result.append(chr(ord("A") + column_number % 26))
            column_number //= 26

        return "".join(reversed(result))
```

## Complexity

| Time         | Space        |
| ------------ | ------------ |
| O(log\_26 n) | O(log\_26 n) |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
