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, return the number of that column, counting the first column, A, as 1. So Z is column 26, AA is column 27 and AB is column 28. The title has 1 to 7 letters and goes no further than "FXSHRXW".

Example 1
Input: columnTitle = "A"
Output: 1
Example 2
Input: columnTitle = "AB"
Output: 28
Explanation: 1 × 26 + 2.
Example 3
Input: columnTitle = "ZY"
Output: 701
Solution

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.

The title ZY: Z has value 26 in the 26s place and Y has value 25 in the ones place, so ZY is 26 × 26 + 25 = 701. Reading left to right, the result goes from 0 to 26 to 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 is stored as a number, and the capital letters A to Z have the codes 65 to 90 in a row. Subtracting 'A' gives 0 for A and 25 for Z, and adding 1 shifts that to 1 for A and 26 for Z.

The + 1 matters, because the letters have no zero. Without it every letter counts one too low: "A" comes out as 0, and "AB" as 0 × 26 + 1 = 1 instead of 28.

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 int stops at 2,147,483,647, and that is exactly the number of the column "FXSHRXW". The problem allows no title past that one, so the method never goes past the limit. One column further, "FXSHRXX", would need 2,147,483,648, and the int result wraps around to a negative number. Code that must accept longer titles can keep result in a long instead.

Java Solution

The method titleToNumber works in these steps:

  1. First, set result to 0, since no letter has been read yet.
  2. Walk over the title with a loop whose index i goes from the first letter to the last, taking each letter with columnTitle.charAt(i).
  3. Turn the letter into value, from 1 for A to 26 for Z, with charAt(i) - 'A' + 1.
  4. Multiply result by 26 and add value.
  5. After the last letter, result holds the column number, and the method returns it.

The main method runs the three examples, the edges where a title gains a letter (Z, AA, ZZ, AAA), the largest title that fits an int, and the one just after it.

Java
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:

Output
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 int. The last line shows what one more column does: "FXSHRXX" should be 2,147,483,648, but the int wraps around and prints -2147483648. The same title read into a long gives 2147483648, so this line marks the end of the titles the method can handle.

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 columnTitle once, with one multiplication and one addition per letter.
  • Space: O(1), because the method keeps only result, value and the index i, and reads letters with charAt instead of copying the title into an array.

Going the other way, from a number to its title, is harder because there is no zero letter: see Excel Sheet Column Title.