168. Excel Sheet Column Title - Explanation

Problem Link

Description

You are given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

Example 1:

Input: columnNumber = 32

Output: "AF"

Example 2:

Input: columnNumber = 53

Output: "BA"

Constraints:

  • 1 <= columnNumber <= ((2^31)-1)


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Number Base Conversion - Converting numbers between different bases (especially base-26)
  • Modular Arithmetic - Using modulo and integer division to extract digits
  • Recursion - Breaking down problems into smaller subproblems with base cases
  • ASCII/Character Manipulation - Converting between characters and their numeric values

1. Recursion

Intuition

Excel columns use a bijective base-26 system where A=1, B=2, ..., Z=26. Unlike standard base conversion where digits range from 0 to base-1, here digits range from 1 to 26. This means we need to adjust by subtracting 1 before finding each digit.

After subtracting 1, we can use modulo 26 to find the rightmost character and divide by 26 to get the remaining prefix. Recursion handles this naturally: first solve for the prefix (if any), then append the current character.

Algorithm

  1. Base case: if columnNumber is 0, return an empty string.
  2. Subtract 1 from columnNumber to convert to 0-indexed.
  3. Recursively call for n // 26 to get the prefix string.
  4. Compute the current character as chr('A' + n % 26).
  5. Return the prefix concatenated with the current character.
class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        if columnNumber == 0:
            return ""

        n = columnNumber - 1
        return self.convertToTitle(n // 26) + chr(n % 26 + ord('A'))

Time & Space Complexity

  • Time complexity: O(logn)O(\log n)
  • Space complexity: O(logn)O(\log n) for recursion stack.

Where nn is the given column number.


2. Iteration

Intuition

The iterative approach works from right to left, building the result string in reverse. At each step, we extract the rightmost character, then reduce the number for the next iteration. Since we build characters from least significant to most significant, we reverse the result at the end.

This avoids recursion overhead and makes the process explicit: subtract 1, find the character via modulo, divide by 26, and repeat until the number becomes 0.

Algorithm

  1. Initialize an empty list res to collect characters.
  2. While columnNumber > 0:
    • Decrement columnNumber by 1.
    • Compute the offset as columnNumber % 26.
    • Append the character chr('A' + offset) to res.
    • Divide columnNumber by 26.
  3. Reverse res and join to form the final string.
class Solution:
    def convertToTitle(self, columnNumber: int) -> str:
        res = []
        while columnNumber > 0:
            columnNumber -= 1
            offset = columnNumber % 26
            res += chr(ord('A') + offset)
            columnNumber //= 26

        return ''.join(reversed(res))

Time & Space Complexity

  • Time complexity: O(logn)O(\log n)
  • Space complexity: O(1)O(1) extra space.

Where nn is the given column number.


Common Pitfalls

Treating It as Standard Base-26 Conversion

Unlike standard base conversion where digits range from 0 to 25, Excel columns use a 1-indexed system where A=1 and Z=26. Forgetting to subtract 1 before taking modulo will cause off-by-one errors. For example, column 26 should be "Z", but without the subtraction, you would incorrectly compute the character for position 0.

Forgetting to Reverse the Result in Iterative Approach

When building the string iteratively by repeatedly taking modulo and dividing, you generate characters from least significant (rightmost) to most significant (leftmost). If you forget to reverse the accumulated characters at the end, "ABC" would incorrectly become "CBA". Either build the string in reverse or explicitly reverse it before returning.