LeetCode #171 Easy

Excel Sheet Column Number

Convert an Excel column title to its corresponding integer column number.

Constraints
  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].
mathstring
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

Skip overflow handling

Column titles are short enough that results fit comfortably in a 32-bit integer. No special handling is required.

5

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.

6

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.

04

Solution & live demo

▶1class Solution:
▶2 def titleToNumber(self, columnTitle:
▶3 str) -> int:
▶4 result = 0
▶5 for ch in columnTitle:
▶6 value = ord(ch) - ord('A') + 1
▶7 result = result * 26 + value
▶8 return result
05

Common pitfalls

Using a zero-based digit value

✗ Wrong
value = ord(ch) - ord('A')
✓ Right
value = ord(ch) - ord('A') + 1

A 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

✗ Wrong
for i, ch in enumerate(reversed(columnTitle)):
    result += value * (26 ** i)
✓ Right
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

✗ Wrong
for ch in reversed(columnTitle):
    result = result * 26 + value
✓ Right
for 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.

06

Edge cases

columnTitle == 'A'

Single letter, value = 0 + 1 = 1, result = 0*26 + 1 = 1.

columnTitle == 'Z'

Single letter, value = 25 + 1 = 26, result = 26 - the boundary case that plain base-26 (without +1) would get wrong.

columnTitle == 'AA'

First letter gives result = 1, second letter gives result = 1*26 + 1 = 27, correctly distinct from 'Z' = 26.

long titles near the integer limit

The same left-to-right multiply-and-add loop scales to any length without special-casing, bounded only by input size.

07

Complexity

Time
O(n)
Space
O(1)
n is the length of the column title; one pass with constant extra space.