Excel Sheet Column Number
Turn a spreadsheet column title such as AB or ZY into the number of that column.
Spreadsheet columns are named with capital letters: A, B and so on up to Z, then AA, AB, and later AAA. Given such a name, columnTitle
Example 1
Output: 1
Example 2
Output: 28
Explanation: 1 × 26 + 2.
Example 3
Output: 701
The Idea
Count the columns of a spreadsheet out loud. The single letters run out at Z, the 26th column. The next column has to use two letters, so it is AA, number 27, then AB, number 28, then AC, and so on to AZ, number 52.
Ordinary digits run out in the same way. After 9, the digits run out, and the next number, 10, uses two places. The 1 in 10 means "one full round of ten". In AB, the first A means "one full round of 26 columns", and B adds 2 more: 1 × 26 + 2 = 28, as Example 2 says.
There is one difference. Ordinary digits start at 0, but no letter stands for zero: A is 1 and Z is 26. That is why Z is a single letter worth 26, where ten needs the two digits of 10.
Now take Example 3, "ZY". The first letter, Z, has the value 26 and stands in the 26s place, so it counts 26 rounds of 26 columns, which is 676. Y has the value 25 and stands in the ones place. Look at the left side of the picture, under the title "ZY": 26 × 26 + 25 = 701.
For a title of seven letters, working out the place of each letter means powers of 26 up to 266. Is there a way to avoid them? The right side of the picture, "reading left to right", shows one. The result starts at 0. Reading Z gives 0 × 26 + 26 = 26. Reading Y multiplies those 26 by 26, which moves Z up into the 26s place, and adds 25, giving 701.
So each new letter pushes everything read so far one place to the left, by multiplying by 26, and then fills the ones place with its own value. Reading the decimal number 123 works the same way, with 10 instead of 26: 1, then 1 × 10 + 2 = 12, then 12 × 10 + 3 = 123. Counting in rounds of 26 like this is called a base-26 number system, and the column titles use it with digits from 1 to 26 instead of 0 to 25.
Solution Approach
Three things need settling: the value of one letter, how the letter values combine, and how large the result can grow.
The value of one letter
How does a program know that Y is 25? In Java a char'A'
The + 1
Combining the letters
What happens to the result for each letter? It is multiplied by 26 and the letter's value is added, exactly as in the rows of the picture. One pass from left to right is enough, with one multiplication and one addition per letter.
Staying inside an int
Can the result get too big? A Java intintresultlong
Java Solution
The method titleToNumber
- First, set
to 0, since no letter has been read yet.result - Walk over the title with a loop whose index
goes from the first letter to the last, taking each letter withi .columnTitle.charAt(i) - Turn the letter into
, from 1 for A to 26 for Z, withvalue .charAt(i) - 'A' + 1 - Multiply
by 26 and addresult .value - After the last letter,
holds the column number, and the method returns it.result
The mainint
public class ExcelSheetColumnNumber {
public static int titleToNumber(String columnTitle) {
int result = 0;
for (int i = 0; i < columnTitle.length(); i++) {
int value = columnTitle.charAt(i) - 'A' + 1; // A = 1, B = 2, ..., Z = 26
result = result * 26 + value; // shift one place, add the letter
}
return result;
}
public static void main(String[] args) {
String[] titles = {"A", "AB", "ZY", "Z", "AA", "ZZ", "AAA", "FXSHRXW", "FXSHRXX"};
for (String title : titles) {
System.out.println(title + " -> " + titleToNumber(title));
}
}
}
Running it prints:
A -> 1
AB -> 28
ZY -> 701
Z -> 26
AA -> 27
ZZ -> 702
AAA -> 703
FXSHRXW -> 2147483647
FXSHRXX -> -2147483648
The title "ZY" gives 701, the same number as the row "read Y (25)" in the picture. Columns Z and AA sit side by side on the sheet, and the output numbers them 26 and 27. ZZ and AAA do the same with 702 and 703: each time the letters run out, a new place starts with A.
The title "FXSHRXW" gives 2147483647, the largest intintlong
Complexity
Reading a title costs the same small step for every letter, and only the running number is kept.
- Time: O(L), because the loop visits each of the L letters of
once, with one multiplication and one addition per letter.columnTitle - Space: O(1), because the method keeps only
,result and the indexvalue , and reads letters withi instead of copying the title into an array.charAt
Going the other way, from a number to its title, is harder because there is no zero letter: see