Community Problem
Excel Sheet Column Number
Difficulty: Easy
Convert a spreadsheet column title (`A`, `Z`, `AA`, `AB`, ...) into its 1-based column number using bijective base-26.
Excel Sheet Column Number
Convert a spreadsheet column title (`A`, `Z`, `AA`, `AB`, ...) into its 1-based column number using bijective base-26.
By @khalidcooper
April 27, 2026
·
Updated May 18, 2026
1,177 views
19
Rate
I once shipped a CSV importer that kept off-by-one'ing on AA, and the bug led directly to this question on a Square onsite. The wrinkle is that this is BIJECTIVE base-26, not regular base-26: there is no zero digit, so Z + 1 == AA, not BA. Once you internalize that, the loop is two lines.
Excel Sheet Column Number
Given a string columnTitle that represents the column title as it appears in an Excel sheet, return its corresponding column number.
For example:
columnTitle number
A 1
B 2
C 3
...
Z 26
AA 27
AB 28
...Examples
Example 1:
- Input:
columnTitle = "A" - Output:
1
Example 2:
- Input:
columnTitle = "AB" - Output:
28 - Explanation:
1 * 26 + 2 == 28.
Example 3:
- Input:
columnTitle = "ZY" - Output:
701 - Explanation:
26 * 26 + 25 == 701.
Example 4:
- Input:
columnTitle = "FXSHRXW" - Output:
2147483647 - Explanation: This is exactly
2^31 - 1, the largest 32-bit signed integer.
Constraints
1 <= columnTitle.length <= 7.columnTitleconsists only of uppercase English letters.columnTitleis in the range["A", "FXSHRXW"].
Follow-up
Why is this not standard base-26? Because there is no zero digit. A represents 1, not 0, so AA is 1 * 26 + 1 = 27, not 1 * 26 + 0 = 26. The conversion is the same loop as base-26 but with each digit value (c - 'A' + 1) instead of (c - '0').
Solution
Hints
Solution Walkthrough
Approach: Bijective Base-26 Left-to-Right Accumulator (O(L) time, O(1) space)
Treat the title as a number written in base 26 where the digits are A=1, B=2, ..., Z=26. There is no zero digit, which is what makes the system bijective: every positive integer has exactly one representation. The conversion is the same Horner-style loop you would write for any positional system: multiply the running total by the base and add the next digit.
Key Insight
Walk-through for AB
start total = 0
read 'A': digit = 1; total = 0 * 26 + 1 = 1
read 'B': digit = 2; total = 1 * 26 + 2 = 28
answer = 28
Walk-through for ZY
start total = 0
read 'Z': digit = 26; total = 0 * 26 + 26 = 26
read 'Y': digit = 25; total = 26 * 26 + 25 = 701Algorithm
- Initialize
total = 0. - For each character
cincolumnTitle, setdigit = (c - 'A' + 1)andtotal = total * 26 + digit. - Return
total.
Why It Works
If the title is c0 c1 ... c_{L-1} with digit values d0 d1 ... d_{L-1} (each in 1..26), the value is sum_{i=0}^{L-1} d_i * 26^{L-1-i}. Horner's method computes this as ((d0 * 26 + d1) * 26 + d2) * 26 + ..., which is exactly what the loop does. Constraints cap the answer at 2^31 - 1 so 32-bit arithmetic suffices.
Complexity
Metric Value
Time O(L) where L = columnTitle.length (<= 7)
Space O(1)Pitfalls
- Treating the system as zero-indexed.
Ais1, not0. SubtractingAand forgetting the+ 1yields off-by-one errors that are obvious only at multi-letter boundaries (AA,AZ, etc.). - Right-to-left accumulation with
pow(26, i). Works but allocates extra multiplications and is harder to extend if the input were a stream. The Horner loop avoids both. - Iterating with
for...ofover the string in JS Sandpack. The ES5 transpile in Sandpack handlesfor...ofon strings correctly, but the indexedforloop withcharCodeAtis more defensive and runs identically.
Solution Code
