Excel Sheet Column Title

Easy
#0168Time: O(log_{26}(N))Space: O(log_{26}(N))5 companies
Patterns
Data structures

Prompt

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 = 1
Output: "A"

Example 2:

Input: columnNumber = 28
Output: "AB"

Example 3:

Input: columnNumber = 701
Output: "ZY"

 

Constraints:

  • 1 <= columnNumber <= 231 - 1

Approaches

2 approaches with complexity analysis and trade-offs.

This approach treats the problem as a base conversion from base-10 to a modified base-26 system. A recursive function is a natural way to implement this. The key insight is that this isn't a standard base-26 system (with digits 0-25), but one with digits 1-26. To handle this, we can convert the number to a 0-indexed system at each step by subtracting 1 before performing the modulo and division operations. The function calculates the rightmost character and then calls itself to find the prefix.

Algorithm

The recursive solution is based on the idea of base conversion. The problem is equivalent to converting a number from base-10 to a special base-26 system where digits are 'A' through 'Z' (representing 1 through 26).

  1. Base Case: The recursion terminates when the columnNumber becomes 0. In this case, an empty string is returned.
  2. Recursive Step: For any columnNumber > 0: a. To handle the 1-based nature of Excel columns (A=1, Z=26) versus the 0-based nature of modulo arithmetic, we first decrement columnNumber by 1. This maps the range [1, 26] to [0, 25]. b. The last character of the Excel title corresponds to (columnNumber - 1) % 26. We convert this numeric value (0-25) to its character representation ('A'-'Z'). c. The preceding part of the title is found by recursively calling the function with the quotient (columnNumber - 1) / 26. d. The final result is constructed by concatenating the result of the recursive call (the prefix) with the character found in step 2b.

Walkthrough

The function convertToTitle(n) works as follows: If n is 0, it returns an empty string, which is our base case. Otherwise, it calculates the last character. This is done by (n - 1) % 26. The -1 is crucial because it maps 1->A, 26->Z correctly to 0->A, 25->Z. The rest of the number is then processed by a recursive call with (n - 1) / 26. The result of this recursive call is the prefix of our final string, to which we append the character we just calculated.

For example, convertToTitle(28):

  1. n=28. (28-1)%26 = 1, which is 'B'.
  2. Recursive call with (28-1)/26 = 1.
  3. convertToTitle(1): (1-1)%26 = 0, which is 'A'.
  4. Recursive call with (1-1)/26 = 0.
  5. convertToTitle(0) returns "".
  6. The call for n=1 returns "" + 'A' -> "A".
  7. The call for n=28 returns "A" + 'B' -> "AB".
class Solution {    public String convertToTitle(int columnNumber) {        if (columnNumber == 0) {            return "";        }        // Decrement to map to a 0-25 range        columnNumber--;        // Recursively find the prefix and append the current character        return convertToTitle(columnNumber / 26) + (char)('A' + (columnNumber % 26));    }}

Complexity

Time

O(log_{26}(N))

Space

O(log_{26}(N))

Trade-offs

Pros

  • The code is very concise and elegant.

  • It directly models the mathematical recurrence relation of the problem.

Cons

  • Incurs overhead from recursive function calls, which can be slightly slower than an iterative solution.

  • For extremely large inputs (beyond the problem's constraints), it could risk a stack overflow error.

Solutions

public class Solution {    public string ConvertToTitle(int columnNumber) {        StringBuilder res = new StringBuilder();        while (columnNumber != 0) {            --columnNumber;            res.Append((char)('A' + columnNumber % 26));            columnNumber /= 26;        }        return new string(res.ToString().Reverse().ToArray());    }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.