Excel Sheet Column Number
Excel Sheet Column Number is LeetCode 171 (Easy). Given a column title as it appears in a spreadsheet header, such as "A", "Z", "AA" or "AB", return its column number. A is 1, Z is 26, AA is 27, and so on.
- The title has 1 to 7 uppercase letters.
- The largest title is
"FXSHRXW", which is 2³¹ − 1, so the answer always fits in a 32-bit int.
- 1 <= columnTitle.length <= 7
- columnTitle consists only of uppercase English letters
- columnTitle is in the range ["A", "FXSHRXW"]
Intuition
Read the title the way you read a decimal number. For "472" you go 4, then 4 × 10 + 7 = 47, then 47 × 10 + 2 = 472. Each new digit shifts everything before it one place left.
Column titles are base 26, with the letters A to Z standing for the digits 1 to 26. So the same loop works with 26 in place of 10: result = result × 26 + value, where value = letter − 'A' + 1.
There is no zero digit here, which is why Z (26) is followed by AA (27) rather than by something like "A0".
A string of symbols that stands for a number in some base, read most significant first. The answer is a running total that is multiplied by the base and then gets the next symbol's value added.
Approach
Before reading on, work out "AB" by hand, then "BA". Which letter is worth more, and why? Aim for one pass with O(1) extra space.
Start from zero
Set result = 0. An empty prefix of the title is worth nothing, and multiplying zero by 26 keeps it zero, so the first letter simply becomes the result.
Turn each letter into its value
Compute ord(ch) - ord('A') + 1, which gives 1 for A and 26 for Z. These are the digit values of this number system, the way 0 to 9 are for decimal.
Shift and add
Set result = result * 26 + value. Multiplying by 26 moves every letter read so far one place to the left, which is exactly what happens when one more letter is written after it.
Return the total
After the last letter, result is the column number. The largest title is 2³¹ − 1, and each partial result is smaller than the final one, so no step overflows a 32-bit int.
Code in Python, C++ and Java
+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.+ 1 in the code is what makes A worth 1 instead of 0.Common pitfalls
Giving A the value 0
value = ord(ch) - ord("A")value = ord(ch) - ord("A") + 1That reads the title as ordinary base 26 with digits 0 to 25. "A" returns 0 instead of 1, and "AB" returns 1 instead of 28.
Adding the letter values without shifting
result += value
result = result * 26 + value
Plain addition ignores position. "AB" and "BA" both come out as 3, but they are columns 28 and 53.
Complexity
Excel Sheet Column Number FAQ
Can I add up powers of 26 from the right instead?
Yes. Going right to left, the last letter is worth value × 1, the one before it value × 26, then value × 26², and so on. It gives the same answer but needs a power that grows each step. The left-to-right loop gets the same powers for free, because each earlier letter is multiplied by 26 once per letter that follows it.
How do I find a column's number in Excel itself?
Use the COLUMN function on any cell in that column: =COLUMN(AB1) returns 28. This is handy for checking your own answers on large titles.
How do I go from the number back to the letters?
That is the reverse problem, Excel Sheet Column Title. It is a little harder, because the missing zero digit means you subtract 1 before every division.