Excel Sheet Column Title (LeetCode 168): Solution and Explanation

Easy
Math
String

Convert a column number to its Excel title, such as 28 to "AB" and 701 to "ZY". It is base 26 with digits 1 to 26 instead of 0 to 25, so subtract 1 before each division.

Intuition

Titles count like an odometer with 26 symbols: A..Z, then AA..AZ, BA..ZZ, AAA and so on. That suggests base 26, where the last letter is n % 26 and the rest is n // 26.

The catch is that there is no zero digit. A is 1 and Z is 26, so plain base 26 breaks on Z: 26 % 26 = 0, and 26 // 26 = 1 would add a spurious leading A, giving "A@" instead of "Z".

Subtracting 1 first fixes it. After n -= 1, the last letter's value 1..26 becomes 0..25, so (n - 1) % 26 picks the letter directly and (n - 1) // 26 is exactly the number the remaining letters to the left must spell.

Approach: Bijective base 26 with a shift by one

  1. Set n to columnNumber and start an empty list of letters.
  2. While n > 0, decrement n so the last letter becomes a 0-based remainder.
  3. Append chr('A' + n % 26).
  4. Set n = n // 26, which removes that letter.
  5. The letters come out last-first, so reverse them and join.

Step-by-step example

columnNumber = 701
  1. n = 701. n -= 1 gives 700. 700 % 26 = 24, which is Y. 700 // 26 = 26.
  2. n = 26. n -= 1 gives 25. 25 % 26 = 25, which is Z. 25 // 26 = 0.
  3. n = 0 stops the loop. letters = [Y, Z], reversed to "ZY".
  4. Check: Z × 26 + Y = 26 × 26 + 25 = 701.

Code: Python, Java, C++

1class Solution:
2    def convertToTitle(self, columnNumber: int) -> str:
3        n = columnNumber
4        letters = []
5        while n > 0:
6            n -= 1
7            letters.append(chr(ord('A') + n % 26))
8            n //= 26
9        return "".join(reversed(letters))
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(log n)

Each iteration divides n by 26, so there is one iteration per letter, at most 7 for 2^31 - 1.

Space

O(log n)

The letters list holds one character per letter of the title.

Other approaches

Recursion (time O(log n), space O(log n))

convertToTitle(n) = convertToTitle((n - 1) // 26) + chr('A' + (n - 1) % 26), with an empty string for n = 0. It builds the title left to right with no reverse, but uses O(log n) stack frames.

Special-case a zero remainder (time O(log n), space O(log n))

Use plain n % 26, and when it is 0 emit Z and subtract one from the quotient. It works, but it is the same shift written as a branch, and the branch is easy to get wrong.

Common mistakes

  • Taking n % 26 without subtracting 1, which turns 26 into "A@" or a wrong carry instead of "Z".
  • Subtracting 1 only once at the start instead of on every iteration. The missing zero digit affects every position.
  • Forgetting to reverse: the first letter computed is the rightmost one.
  • In Java or C#, adding an int to 'A' gives an int; cast it back to char.

Frequently asked questions

Why does subtracting 1 work at every position?

Write n = d_k × 26^k + ... + d_1 × 26 + d_0 with each d in 1..26. Then n - 1 = (d_k × 26^(k-1) + ... + d_1) × 26 + (d_0 - 1), and d_0 - 1 is in 0..25. So (n - 1) % 26 = d_0 - 1 and (n - 1) // 26 is the number spelled by the remaining letters, which has the same form.

How is this the inverse of Excel Sheet Column Number?

Column Number computes result = result × 26 + value(letter) left to right. This problem undoes that one letter at a time from the right, using the shift by one to recover each value.

What is the longest possible title?

2^31 - 1 is "FXSHRXW", seven letters, so the loop runs at most seven times.