AlgoMaster Logo

Excel Sheet Column Title

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

This looks like a base-26 conversion: take a number, convert it to base 26, and map each digit to a letter. The complication is that the numbering does not start at zero.

Standard number systems are 0-indexed. In base 10, digits go from 0 to 9. In base 26, you would expect digits from 0 to 25. Excel columns are 1-indexed: they go from A (1) to Z (26), with no digit representing 0. There is no "zero" column.

So a plain columnNumber % 26 is wrong. When columnNumber is a multiple of 26 (like 26, 52, or 702), the modulo gives 0, but the correct letter is Z, which represents 26. The fix is to subtract 1 before each modulo operation, shifting from 1-indexed to 0-indexed at each step. (columnNumber - 1) % 26 produces a value from 0 to 25, which maps to A through Z.

Key Constraints:

  • 1 <= columnNumber <= 2^31 - 1 -> The number can be up to about 2.1 billion. Any approach that processes one digit at a time runs in O(log_26(n)) time, which is at most about 7 iterations.
  • The numbering is 1-indexed with no zero symbol. This is a bijective base-26 system, requiring a subtract-1 adjustment before each modulo operation.
  • The output is a string of uppercase letters A-Z. The length grows logarithmically with the input.

Approach 1: Iterative (Modified Base-26 Conversion)

Intuition

To convert a number to a different base, repeatedly take the remainder when dividing by the base, then divide the number by the base. For base 10, n % 10 gives the last digit, then n = n / 10 moves to the next digit.

Excel columns work the same way with base 26, plus the 1-indexing adjustment. Before taking the modulo, subtract 1 from the number. This converts the 1 to 26 range into 0 to 25, which maps to A through Z. After getting the remainder, divide the adjusted number by 26 and repeat until the number reaches 0.

Since we extract digits from the least significant position first (rightmost character), we build the result in reverse and flip it at the end.

Algorithm

  1. Initialize an empty result string.
  2. While columnNumber is greater than 0:
    • Subtract 1 from columnNumber (shift from 1-indexed to 0-indexed).
    • Compute remainder = columnNumber % 26.
    • Append the character corresponding to remainder (0 maps to 'A', 1 maps to 'B', ..., 25 maps to 'Z').
    • Update columnNumber = columnNumber / 26.
  3. Reverse the result string and return it.

Example Walkthrough

1Start: columnNumber = 701, result is empty
1/5

Code

The iterative approach builds the string backwards and reverses it at the end. Recursion can produce the characters in the correct order directly, without a separate reversal step.

Approach 2: Recursive

Intuition

Instead of building the string from the rightmost character and reversing, recursion can handle the higher-order digits first. Before appending the current character, recursively process the remaining quotient. Because the recursive call runs before the append, the higher-order characters land in the string ahead of the current one, so the result comes out in left-to-right order.

The recursion mirrors how place value works: the quotient columnNumber / 26 holds every digit except the last, and columnNumber % 26 is the last character. Computing the prefix first, then appending the last character, places each character in its correct position with no reversal.

Algorithm

  1. Base case: if columnNumber is 0, return an empty string.
  2. Subtract 1 from columnNumber.
  3. Recursively compute the title for columnNumber / 26 (the "prefix" of the result).
  4. Append the character for columnNumber % 26.
  5. Return the concatenated string.

Example Walkthrough

1Call convertToTitle(701): 701-1=700, remainder=24->'Y', recurse with 26
1/6

Code