Excel Sheet Column Number
Easy
Topics
Given a spreadsheet column title s (as in Excel: A, B, ..., Z, AA, AB, ...), return its corresponding column number.
Example 1
A comes to 1.Input: s = "A" Output: 1
Example 2
Input: s = "AB" Output: 28
Example 3
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
- Start the accumulator at 0.
- For each character left to right, map it to 1..26 with
ord(c) - ord('A') + 1, thenr = r * 26 + value. - 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 exactly2^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 ** positionper 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) % 26andn = (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 rimport java.util.*;
class Solution {
public int titleToNumber(String s) {
int r = 0;
for (int i = 0; i < s.length(); i++) r = r * 26 + (s.charAt(i) - 'A' + 1);
return r;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int titleToNumber(string s) {
int r = 0;
for (char c : s) r = r * 26 + (c - 'A' + 1);
return r;
}
};/**
* @param {string} s
* @return {number}
*/ var titleToNumber = function(s) {
let r = 0;
for (const c of s) r = r * 26 + (c.charCodeAt(0) - 64);
return r;
};