Excel Sheet Column Number (LeetCode 171): Solution and Explanation
Spreadsheet columns are labeled A, B, and so on up to Z, then AA, AB, and onward. Given a column label, return the column's position as an integer, so A is 1, Z is 26 and AA is 27.
Watch it run step by step
Try your own input and write the code →Intuition
A brute force idea is to generate labels in order, A, B, C and so on, counting until you reach the target. That works for short labels but the count grows by a factor of 26 with every extra letter, so it is hopeless for anything like "FXSHRXW", which maps to the largest 32-bit integer.
The better way is to notice that a column label is just a number written in base 26. Each letter is a digit, and the leftmost letter is the most significant. The twist is that the digits run from 1 to 26 instead of 0 to 25, because there is no letter for zero. That is why AA comes right after Z: Z is 26, and AA is 1 × 26 + 1 = 27.
Once you see it as positional notation, the conversion is the same one you would use to parse a decimal string. Start with 0, and for each character multiply the running result by the base and add the digit's value. For decimal you add ord(c) - ord('0'); here you add ord(c) - ord('A') + 1. Horner's method like this avoids computing powers of 26 explicitly and processes the label left to right in one pass.
Approach: Base 26 conversion with Horner's method
- Initialize
resultto 0. - Walk through
columnTitlefrom left to right. - Convert each letter to its value with
ord(char) - ord('A') + 1, so A becomes 1 and Z becomes 26. - Update
result = result * 26 + value, shifting everything seen so far one base 26 place to the left before adding the new digit. - Return
resultafter the last character.
Step-by-step example
columnTitle = "ZY"
- Start with
result = 0. - The first character is 'Z', whose value is 26. Now
result = 0 × 26 + 26 = 26. - The second character is 'Y', whose value is 25. Now
result = 26 × 26 + 25 = 676 + 25 = 701. - There are no more characters, so the function returns 701.
Code: Python, Java, C++
1class Solution:
2 def titleToNumber(self, columnTitle: str) -> int:
3 result = 0
4 for char in columnTitle:
5 result = result * 26 + (ord(char) - ord('A') + 1)
6 return result1class Solution {
2 public int titleToNumber(String columnTitle) {
3 int result = 0;
4 for (char c : columnTitle.toCharArray()) {
5 result = result * 26 + (c - 'A' + 1);
6 }
7 return result;
8 }
9}1class Solution {
2public:
3 int titleToNumber(string columnTitle) {
4 int result = 0;
5 for (char c : columnTitle) {
6 result = result * 26 + (c - 'A' + 1);
7 }
8 return result;
9 }
10};Time and space complexity
O(n)
The loop does constant work for each of the n characters in the label. Since the label is at most seven letters, this is effectively constant time in practice.
O(1)
Only the running result is stored. No extra arrays or strings are created.
Other approaches
Right to left with powers of 26 (time O(n), space O(1))
Process characters from the end, multiplying each letter's value by 26 raised to its position from the right and summing. It is mathematically identical, but it needs a separate running power variable and reads less naturally than the left-to-right update.
Brute force enumeration (time O(26^n), space O(n))
Generate labels in order, A, B, and so on, incrementing a counter until the generated label equals the input. It mirrors how a person might count columns on screen, but the number of labels grows by a factor of 26 for every extra letter, so it is only usable for one- or two-letter inputs and exists mainly as a contrast to the direct conversion.
Common mistakes
- Mapping A to 0. With zero-based letters, "A" becomes 0 and "AA" becomes 26, both off by one. The values must run from 1 to 26.
- Multiplying after adding instead of before. The update must be
result * 26 + value; writing(result + value) * 26shifts the last digit one place too far. - Using a 32-bit integer in Java or C++ without checking the range. The guaranteed maximum fits in a signed 32-bit int, but intermediate values in the reverse problem or a longer label could overflow.
- Confusing this with Excel Sheet Column Title, the reverse problem, which needs
(n - 1) % 26to handle the missing zero digit.
Frequently asked questions
Why is Excel column numbering base 26 without a zero?
Columns use only the 26 letters, and there is no symbol for zero, so each position holds a value from 1 to 26. This scheme is sometimes called bijective base 26, and it means every positive integer has exactly one label.
What column number is AAA?
It is 1 × 26² + 1 × 26 + 1 = 676 + 26 + 1 = 703. Running the loop gives the same result: 1, then 27, then 703.
How do you convert a number back to an Excel column title?
Repeatedly subtract one, take the remainder modulo 26 to get the letter, and divide by 26. The subtraction compensates for the missing zero digit, and you build the letters from right to left.
Why multiply by 26 before adding each letter?
Reading a new letter means every letter already seen moves one place to the left, which multiplies its weight by 26. Multiplying the running total first and then adding the new letter's value applies that shift to all previous letters at once, the same way you parse a decimal string digit by digit.