Excel Sheet Column Number

Easy

Topics
MathString

Given a spreadsheet column title s (as in Excel: A, B, ..., Z, AA, AB, ...), return its corresponding column number.

Example 1

titleA
digit1
Base-26, but 1-indexed: A is 1 rather than 0, so there is no zero digit and the usual place-value formula applies with that offset. A comes to 1.
Top row is the column title; below it each letter’s numeric value.
Input:  s = "A"
Output: 1

Example 2

titleAB
digit12
Input:  s = "AB"
Output: 28

Example 3

titleZY
digit2625
Input:  s = "ZY"
Output: 701

Constraints

  • 1 <= s.length <= 7
  • s consists of uppercase English letters and is in the range ["A", "FXSHRXW"].
Editorial — intuition, approach, complexity and pitfalls

Intuition

This is base conversion, with one wrinkle that catches people. Excel columns run A..Z, then AA..AZ, BA.., so it looks like base 26 — but it is bijective base 26, which has no zero digit. A is 1, not 0, and Z is 26.

That matters because ordinary base 26 would make "AA" equal 0*26 + 0 = 0, whereas Excel says 27. With digits running 1..26 instead of 0..25, every position contributes at least 1, so there is no leading-zero ambiguity and every positive integer has exactly one representation.

Once the digit values are right, it is Horner's method, exactly as for decimal: process left to right, multiply the accumulator by the base, add the new digit.

"ZY"  ->  r = 0
          'Z':  r = 0*26 + 26 = 26
          'Y':  r = 26*26 + 25 = 701

💡 Horner's method builds a positional number in one pass without computing any powers: each step shifts what you have by one place and adds the next digit. It avoids both pow calls and the need to know the length up front.

Approach

  1. Start the accumulator at 0.
  2. For each character left to right, map it to 1..26 with ord(c) - ord('A') + 1, then r = r * 26 + value.
  3. Return the accumulator.

Dry Run

"A"   'A'=1                     r = 0*26 + 1  = 1
"AB"  'A'=1                     r = 0*26 + 1  = 1
      'B'=2                     r = 1*26 + 2  = 28
"ZY"  'Z'=26                    r = 0*26 + 26 = 26
      'Y'=25                    r = 26*26 + 25 = 701
"Z"   'Z'=26                    r = 26

"AB" is the one that shows the bijective base at work: 28, not 2. The leading A contributes a full 26 because it is the digit 1 in the 26s place, not a zero.

Complexity

  • Time — O(n): one pass over the title, constant work per character. The constraint caps the length at 7, so this is at most seven iterations.
  • Space — O(1): a single accumulator. Note the maximum input "FXSHRXW" evaluates to 2,147,483,647, which is exactly 2^31 - 1 — the problem is deliberately built to sit on the signed 32-bit boundary, so the accumulator must be a 32-bit int at minimum and anything larger would overflow. Worth checking rather than assuming the range is comfortable.

Common Pitfalls

  • Using ord(c) - ord('A') and forgetting the + 1. That is ordinary base 26 with a zero digit, and it gives 0 for "A" and 26 for "AA" instead of 1 and 27.
  • Processing right to left while still multiplying the accumulator by 26. That reverses the place values; going right to left needs an explicit running power instead.
  • Computing 26 ** position per character. Correct, but it needs the length first and does more arithmetic than Horner, and in a language with fixed-width integers it is easier to overflow mid-computation.
  • Assuming the result fits comfortably in an int because the string is short. Seven characters reach exactly 2^31 - 1.
  • Treating it as alphabet indexing rather than a number system. The follow-up — converting a number back to a title — is where that bites: the inverse needs (n - 1) % 26 and n = (n - 1) // 26, and the off-by-one only makes sense if you know the base is bijective.

Full Solution — all languages

class Solution:
    def titleToNumber(self, s):
        r = 0
        for c in s:
            r = r * 26 + (ord(c) - ord('A') + 1)
        return r
SensAI
Beta
Tap the mic to stop voice mode