◀ THE GRIND — MATH & GEOMETRY

Excel Sheet Column Title

The drill: Translate a spreadsheet column's 1-based position into its letter name — column 1 is A, column 26 is Z, column 27 rolls over into AA. It's counting in a base-26 system that has no zero digit.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A single positive integer arrives — the 1-based position of a spreadsheet column — and the job is to translate it into the letter name that column would actually carry, the way A1-style spreadsheet headers work.

Column 1 is A, column 26 is Z, and column 27 rolls over the way a car odometer would, becoming AA; the letters keep climbing and widening exactly like that as the number grows, with no digit standing in for zero anywhere in the alphabet.

The result is that letter string itself, built from the uppercase alphabet only, matching however many letters the sheet's own naming scheme would use for that position.

EX 01
columnNumber = 1
"A"
FIRST COLUMN
EX 02
columnNumber = 26
"Z"
LAST SINGLE LETTER
EX 03
columnNumber = 27
"AA"
ROLLOVER INTO TWO LETTERS
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

A plain base-26 conversion assumes a zero digit exists, but spreadsheet letters run A through Z — 1 through 26, never 0 through 25. What breaks if you just divide and mod by 26 directly?

HINT 2 THE STRUCTURE

Simulating the sheet's own counting — A, B, … Z, AA, AB, … — sidesteps the missing zero entirely: each step just increments the last letter and carries when it rolls past Z, like an odometer with no 0.

HINT 3 ONE STEP FROM THE ANSWER

Subtract one before taking the remainder: (n − 1) mod 26 lands cleanly in A..Z, then floor-divide (n − 1) by 26 and repeat for the digit to the left. That single −1 is what erases the missing-zero problem.

COACH'S BOARD — THE PATTERN, STEP BY STEP
BASE-26, MINUS ONEPATTERN · BASE-26 WITH NO ZEROcolumnNumber = 27
27
1
LETTERS SO FAR
letters[]
STEP 1

Column 27. Each digit is one less than what it represents — subtract 1 before taking mod 26 to dodge the missing zero digit.

STEP 1 / 5 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/excel-sheet-column-title.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        letters = []
        n = columnNumber
        while n > 0:
            n -= 1                            # shift 1..26 down to 0..25
            letters.append(chr(65 + n % 26))
            n //= 26
        return ''.join(reversed(letters))
TIME O(LOG N)SPACE O(LOG N)PYTHON · RACE PACE · 9 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED