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.
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.
- column numbers range from 1 up into the billions
- output uses uppercase letters A through Z only
- column 1 maps to "A"; there is no digit that represents zero
- letters widen (A, ..., Z, AA, ...) exactly like the sheet's own count-up
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.
Column 27. Each digit is one less than what it represents — subtract 1 before taking mod 26 to dodge the missing zero digit.
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))class Solution:
def convertToTitle(self, columnNumber: int) -> str:
title = "A"
for _ in range(columnNumber - 1): # simulate the sheet's own counting
title = self._increment(title)
return title
def _increment(self, s: str) -> str:
chars = list(s)
i = len(chars) - 1
while i >= 0:
if chars[i] == 'Z':
chars[i] = 'A' # carry, like an odometer with no zero
i -= 1
else:
chars[i] = chr(ord(chars[i]) + 1)
return ''.join(chars)
return 'A' + ''.join(chars) # carried out of the front — grow by one letterclass Solution {
public String convertToTitle(int columnNumber) {
StringBuilder sb = new StringBuilder();
int n = columnNumber;
while (n > 0) {
n--;
sb.append((char) ('A' + n % 26));
n /= 26;
}
return sb.reverse().toString();
}
}class Solution {
public String convertToTitle(int columnNumber) {
StringBuilder title = new StringBuilder("A");
for (int i = 0; i < columnNumber - 1; i++) {
increment(title);
}
return title.toString();
}
private void increment(StringBuilder s) {
int i = s.length() - 1;
while (i >= 0) {
if (s.charAt(i) == 'Z') {
s.setCharAt(i, 'A');
i--;
} else {
s.setCharAt(i, (char) (s.charAt(i) + 1));
return;
}
}
s.insert(0, 'A');
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED