File: excel-sheet-column-title/solution.py

Date: 2026-06-06

Time: 16:30

excel-sheet-column-title/solution.py

Purpose

This file solves LeetCode #168 — Excel Sheet Column Title. It converts a 1-indexed integer into the corresponding Excel column label (A, B, ..., Z, AA, AB, ..., ZZ, AAA, ...). It's the inverse of the excel-sheet-column-number solution elsewhere in this repo.

Key Components

converttotitle(columnNumber: int) -> str — The core algorithm. Takes an integer in [1, 2^31 - 1] and returns the Excel column string.

The function is a modified base-26 conversion. Standard base-26 would use digits 0–25, but Excel columns are 1-indexed (A=1, Z=26, not A=0, Z=25). The columnNumber -= 1 on line 15 shifts from 1-indexed to 0-indexed on each iteration, which is the entire trick.

Flow

Walk through converttotitle(28)"AB":

1. Iteration 1: 28 - 1 = 27. 27 % 26 = 1chr(1 + 65) = 'B'. 27 // 26 = 1.

2. Iteration 2: 1 - 1 = 0. 0 % 26 = 0chr(0 + 65) = 'A'. 0 // 26 = 0.

3. result = ['B', 'A'], reversed → "AB".

Characters are appended least-significant-first, then the list is reversed at the end. This avoids repeated string prepending (which would be O(n) per step).

Patterns

Dependencies

Invariants

Error Handling

None. No input validation, no exceptions. Invalid inputs (≤ 0, non-integer) produce silently wrong results. This is typical for LeetCode solutions where the problem statement guarantees valid input.

Test Coverage

Four test methods cover:

Topics to Explore

Beliefs