Excel Sheet Column Number
Convert an Excel column title to its corresponding integer column number.
- 1 <= columnTitle.length <= 7
- columnTitle consists only of uppercase English letters.
- columnTitle is in the range ["A", "FXSHRXW"].
Intuition
Excel sheet column number converts a column title such as "AB" into its number, 28. This is base-26 conversion, but with one difference that matters more than it first appears.
Ordinary base-26 uses digits 0 through 25. Excel columns use A through Z as 1 through 26, with no zero at all:
- There is no digit representing zero, so the system is bijective base-26 rather than standard base-26.
That is why the sequence runs Z, AA, AB — after 26 comes 27, written AA, whereas standard base-26 would write 10 using a zero digit that does not exist here.
Despite that, the evaluation is the familiar positional loop. Process characters left to right, and for each one:
result = result × 26 + (char − 'A' + 1)
The + 1 is what encodes the bijective system, mapping A to 1 rather than 0. Omitting it maps A to 0, making "A" evaluate to 0 and every multi-character title come out short.
Working left to right avoids tracking powers of 26 explicitly — each step multiplies the accumulated value by 26, which shifts it one position automatically.
The titles are short enough that the result always fits in a 32-bit integer, so no overflow handling is needed.
The reverse conversion, Excel Sheet Column Title, is the harder direction, because the absent zero forces a decrement before each division.
Horner's method: result = result * 26 + digit, left to right. The inverse of the title conversion, and easier because going this direction needs no off-by-one correction — the digit value is simply ch - 'A' + 1.
Approach
Before reading on: price up what the direct approach costs here, then ask what pattern in the numbers removes the loop entirely. Aim for O(n) time and O(1) space.
Recognise the bijective base
Letters map to 1 through 26 with no zero digit, so this is bijective base-26. That absent zero is why the sequence runs Z, AA, AB rather than using a 10.
Process characters left to right
Accumulate with result = result * 26 + value. Each multiplication shifts the running total one position, so no explicit powers of 26 are needed.
Map each letter with an offset
Compute char - 'A' + 1. The + 1 encodes the bijective system — without it A becomes 0 and every title evaluates too low.
Skip overflow handling
Column titles are short enough that results fit comfortably in a 32-bit integer. No special handling is required.
Note the harder reverse direction
Converting a number back to a title must decrement before each division, because the missing zero shifts the remainders. That is Excel Sheet Column Title.
Cost of the conversion
One pass over the title with constant work per character gives O(n) time and O(1) space, where n is the title length.
Solution & live demo
Common pitfalls
Using a zero-based digit value
value = ord(ch) - ord('A')value = ord(ch) - ord('A') + 1A is column 1, not column 0. Without the + 1 every column comes out short and "A" maps to 0, which isn't a valid column at all.
Computing powers explicitly
for i, ch in enumerate(reversed(columnTitle)):
result += value * (26 ** i)result = result * 26 + value
Correct but computes a growing power at each step and needs the string reversed. Horner's form is one multiply-add per character, left to right, with no exponentiation.
Processing right to left without reversing the accumulation
for ch in reversed(columnTitle):
result = result * 26 + valuefor ch in columnTitle:
Horner's method requires the most significant digit first. Feeding it backwards computes the value of the reversed title — "AB" becomes 28 instead of 27.
Edge cases
Single letter, value = 0 + 1 = 1, result = 0*26 + 1 = 1.
Single letter, value = 25 + 1 = 26, result = 26 - the boundary case that plain base-26 (without +1) would get wrong.
First letter gives result = 1, second letter gives result = 1*26 + 1 = 27, correctly distinct from 'Z' = 26.
The same left-to-right multiply-and-add loop scales to any length without special-casing, bounded only by input size.