Problem Statement
Excel Sheet Column Number
Open a spreadsheet and look at the column headers across the top. They go A, B, C, all the way to Z, and then they keep going: AA, AB, AC, and so on. Each header is really just a label for a number. A is column 1, B is column 2, Z is column 26, AA is column 27, AB is column 28. Your job: given one of these letter labels (called columnTitle), figure out the number it stands for. The trick is that this is a counting system that uses 26 symbols (the letters A through Z) instead of the 10 digits (0 through 9) we normally use. We call a system like this base-26, which just means each spot can hold 26 different values instead of 10. The only twist is that the letters run from 1 to 26, not 0 to 25, so A means 1, not 0. To find the answer, we read the letters left to right and build the number up one letter at a time.
Signals to notice
Brute force first
Not applicable — direct computation.
The key insight
result = result × 26 + (char - 'A' + 1). Process left to right. O(n).
Trace it on columnTitle="ZY"
start: result = 0
ch='Z': value = ('Z'-'A')+1 = 26; result = 0*26 + 26 = 26
ch='Y': value = ('Y'-'A')+1 = 25; result = 26*26 + 25 = 676 + 25 = 701
end of string
return result = 701What must stay true
It's base-26 but 1-indexed: A=1, B=2, ..., Z=26. Each position multiplied by 26^distance from right.
Shape of the loop
result = 0
for ch in columnTitle: # left to right
value = (ch - 'A') + 1 # A->1 ... Z->26
result = result * 26 + value
return resultPseudocode only — the full worked solution lives in the Solution tab.
Easy way to go wrong
Using A=0 — Excel columns are 1-indexed.