Spreadsheet Column Label

EasyMath & Geometry

Problem

A spreadsheet labels its columns A, B, ..., Z, then AA, AB, ..., AZ, BA, ..., ZZ, then AAA, and so on.

Given a 1-based column index, return its label.

Example 1

Input:
index = 27
Output:
"AA"

After the 26 one-letter labels comes AA.

Example 2

Input:
index = 52
Output:
"AZ"

Example 3

Input:
index = 703
Output:
"AAA"

26 one-letter labels plus 676 two-letter labels come first.

Constraints

  • 1 ≤ index ≤ 10^15

Hint

Show the first hint

This looks like base 26, but there is no digit for zero: the 'digits' run from A = 1 to Z = 26.

Walkthrough

How to approach it

01Clarify before coding

  • Is the index 1-based? Yes, 1 → A.
  • Uppercase letters only? Yes.
  • How large can it get? Up to 10^15, which needs 11 letters.

02Brute force

Start from "A" and increment the label index - 1 times, like an odometer.

Cost: O(index · length): 10^15 increments.

The rest of the walkthrough is in the app

The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.

This problem is part of Pro.