Skip to content
← All posts

Excel Sheet Column Number: Base-26 to Integer

4 min read
leetcodeproblemeasymathstrings

Excel Sheet Column Number is LeetCode problem 171. Given a string columnTitle that represents a column title in an Excel sheet, return its corresponding column number.

The mapping works like this: A is 1, B is 2, ..., Z is 26, AA is 27, AB is 28, and so on.

Examples:

  • "A" returns 1
  • "AB" returns 28
  • "ZY" returns 701

If you have worked with number bases before, this will feel familiar. Excel column labels are essentially base-26 numbers where the digits run from 1 to 26 instead of 0 to 25.

"ZY" as base-26Zpos 0Ypos 1Z = 26Y = 25Positional breakdownZ (pos 0):26 * 26^1 = 676Y (pos 1):25 * 26^0 = 25676 + 25 = 701Just like decimal: "59" = 5*10 + 9Excel columns: "ZY" = 26*26 + 25 = 701A=1, B=2, ..., Z=26 (base-26 with digits 1..26 instead of 0..25)
Each letter maps to a value 1..26. Multiply by the positional power of 26 and sum, just like reading a base-10 number.

Approach: process left to right with Horner's method

Think about how you read a decimal number like "59". You start from the left: take 5, then shift it left by multiplying by 10 and add 9 to get 59. The formula is result = result * 10 + digit applied one character at a time.

The same logic applies here, but with base 26. Each letter maps to a value: 'A' is 1, 'B' is 2, ..., 'Z' is 26. You compute ord(char) - ord('A') + 1 to get that value. Then you accumulate with:

result = result * 26 + (ord(char) - ord('A') + 1)

This is Horner's method for polynomial evaluation. For a string of length n, the formula expands to:

value = c1 * 26^(n-1) + c2 * 26^(n-2) + ... + cn * 26^0

But you never need to compute powers of 26 explicitly. The left-to-right multiply-and-add loop handles it.

Python solution

def titleToNumber(columnTitle: str) -> int:
    result = 0
    for char in columnTitle:
        result = result * 26 + (ord(char) - ord('A') + 1)
    return result

Walkthrough: "ZY" returns 701

Let's trace through the algorithm to see how "ZY" becomes 701.

Tracing titleToNumber("ZY"):

Step 1: Process 'Z' (first character).

char ="Z"value =ord("Z") - ord("A") + 1 = 26
result =0 * 26 + 26 = 26

result starts at 0. Shift left by multiplying by 26, then add the value of 'Z'. result = 0 * 26 + 26 = 26.

Step 2: Process 'Y' (second character).

char ="Y"value =ord("Y") - ord("A") + 1 = 25
result =26 * 26 + 25 = 701

Shift the accumulated result left by multiplying by 26, then add the value of 'Y'. result = 26 * 26 + 25 = 676 + 25 = 701.

All characters processed. Return result = 701.

"ZY" is column 701 in Excel.

The first character 'Z' has value 26. Starting with result = 0, you compute 0 * 26 + 26 = 26. The second character 'Y' has value 25. You shift result left: 26 * 26 + 25 = 676 + 25 = 701. Done.

Notice how the multiplication by 26 automatically handles the positional weight. When you process 'Y', the earlier value of 26 (from 'Z') gets multiplied by 26, giving it its correct positional weight of 26 * 26^1 = 676.

Complexity

Time: O(n) where n is the length of the input string. You iterate through each character exactly once, doing constant work per character.

Space: O(1). You only store a single integer result regardless of input size.

Building blocks

This problem is a direct application of one reusable building block.

Base conversion with Horner's method

The pattern of processing digits left to right with result = result * base + digit_value is the standard way to convert any positional number system to an integer. You will see this same loop for:

  • Converting binary strings to integers (base 2)
  • Converting octal or hex strings (base 8, base 16)
  • Any custom encoding where symbols map to numeric values
result = 0
for symbol in input_string:
    result = result * base + value_of(symbol)

The only thing that changes between problems is the base and how you map each symbol to its numeric value. Here, the base is 26 and the mapping is ord(char) - ord('A') + 1.

Edge cases

  • Single character: "A" returns 1, "Z" returns 26. The loop runs once and the multiply-by-26 step has no effect since result starts at 0.
  • "Z" specifically: This maps to 26, not 0. Unlike standard base-26 where Z would represent 0, Excel columns use 1-indexed digits (A=1 through Z=26).
  • "AA": This is 27, not 0. A in the tens place contributes 1 * 26 = 26, and A in the ones place contributes 1. So 26 + 1 = 27.
  • Long strings: Excel supports columns up to "XFD" (16384 columns), but the algorithm works for arbitrarily long strings. The only practical limit is integer overflow in languages with fixed-width integers. Python handles arbitrary precision natively.

The key difference from standard base-26 is the digit range. Normal base-26 uses digits 0 through 25. Excel columns use digits 1 through 26. This means there is no zero digit, which is why the reverse problem (integer to column title) requires extra care. But for this direction, the formula works directly with no special handling.

From understanding to recall

You now understand the approach: treat each letter as a base-26 digit and evaluate left to right with Horner's method. But understanding is not the same as being able to write the code from scratch under pressure.

The building block here, base conversion via Horner's method, shows up across many problems. If you can write result = result * base + value from muscle memory, this entire problem reduces to figuring out the base and the digit mapping. Spaced repetition makes that loop automatic. CodeBricks drills you on the building block in isolation, so when you see it in context, you recognize it instantly and write it without hesitation.

Related posts

  • Excel Sheet Column Title - The reverse problem: converting a column number back to its letter title, which requires handling the 1-indexed base-26 system with modular arithmetic
  • Reverse Integer - Another digit-by-digit accumulation problem using the multiply-and-add pattern, but extracting digits right to left with modulo and division