Excel Sheet Column Title
Turn a column number into the letters a spreadsheet shows above that column, such as 28 into AB.
A spreadsheet names its columns with letters: the first 26 are A to Z, and the next ones are AA, AB, and so on. Given a column number columnNumber
Example 1
Output: "A"
Example 2
Output: "AB"
Example 3
Output: "ZY"
The Idea
Scroll right in a spreadsheet and watch the letters above the columns. They run A, B, ... Z, and after Z comes AA, then AB, all the way to AZ. Then BA starts. It looks like a car's mileage counter rolling over, but with letters instead of digits.
There is one difference from a mileage counter. Its digits run 0 to 9, so 9 rolls over to 10, a two-digit number that starts with 1 and ends in 0. The letters here are worth 1 to 26, A being 1 and Z being 26, and there is no letter for 0. So after Z comes AA, at the point where a mileage counter would show 10.
Seen that way, a title is a number written with letters as digits, where each place to the left is worth 26 times more. The title "AB" has A in the 26s place and B in the ones place: 1 × 26 + 2 = 28, which is Example 2. The title "ZY" is 26 × 26 + 25 = 676 + 25 = 701, which is Example 3.
Going the other way is the real question. For a decimal number, the last digit is the remainder after dividing by 10. Can the last letter come from the remainder after dividing by 26? For 28 it works: the remainder is 2, the letter B. For 26 it fails: the remainder is 0, and no letter stands for 0, yet the answer should be Z.
The fix is to subtract 1 before dividing. That moves the last letter's value from 1 to 26 down to 0 to 25, so A is 0, B is 1 and Z is 25, and every remainder now names a letter. The picture below follows 701 through this.
In the row labelled pass 1, 701 becomes 700. Dividing 700 by 26 gives 26, with 24 left over, and A + 24 is Y, the last letter. The quotient, 26, is the number left for the letters before Y, so the dashed arrow carries it down to pass 2.
In pass 2, 26 becomes 25, the remainder is 25, and A + 25 is Z. The quotient is 0, so no letters remain. The letters came out last first, Y and then Z, and reading them backwards gives the title "ZY", as the box at the bottom shows.
Solution Approach
The method has four things to get right: how to read the last letter, what number is left for the letters before it, how to put the letters in order, and whether a large input can overflow.
Reading the last letter
Why not treat the title as an ordinary base-26 number, with A as 0 and no subtraction? Run that version and it returns "B" for 1, "BA" for 26 and "BC" for 28. Every column is off, because A now stands for 0 while columns start at 1.
Subtracting 1 first fixes this. Say the title's last letter is worth some value from 1 to 26, and the letters before it form a number called restrest × 26 + valuerest × 26 + (value - 1)value - 1
The number left for the letters before
What does the division leave for the rest of the title? From that same formula, the quotient of (columnNumber - 1) / 26rest
The repeating stops when the quotient reaches 0, as in pass 2 for 701. Look at 52: one less is 51, and 51 divided by 26 gives remainder 25, a Z, and quotient 1. Then 1 minus 1 is 0, and 0 divided by 26 gives remainder 0, an A, and quotient 0. Read backwards, that is "AZ".
Putting the letters in order
The letters come out from right to left. Collecting them in a StringBuilder
Large inputs and overflow
Could the arithmetic go past what an intintInteger.MAX_VALUElong
Java Solution
The method convertToTitle
- First, create an empty
namedStringBuilder to collect the letters.sb - Loop while
is greater than 0. Inside, subtract 1 fromcolumnNumber , so the last letter's value lies between 0 and 25.columnNumber - Append the letter
to'A' + columnNumber % 26 , cast back to asb .char - Divide
by 26, which leaves the number for the letters before this one, and go round the loop again.columnNumber - When the loop ends, reverse
and return it as a string.sb
The mainInteger.MAX_VALUE
public class ExcelSheetColumnTitle {
public static String convertToTitle(int columnNumber) {
StringBuilder sb = new StringBuilder();
while (columnNumber > 0) {
columnNumber--; // letters are worth 1..26, shift to 0..25
sb.append((char) ('A' + columnNumber % 26));
columnNumber /= 26; // what is left for the letters before it
}
return sb.reverse().toString(); // the last letter was found first
}
public static void main(String[] args) {
int[] tests = {1, 28, 701, 26, 27, 52, 702, 703, 0, Integer.MAX_VALUE};
for (int n : tests) {
System.out.println(n + " -> \"" + convertToTitle(n) + "\"");
}
}
}
Running it prints:
1 -> "A"
28 -> "AB"
701 -> "ZY"
26 -> "Z"
27 -> "AA"
52 -> "AZ"
702 -> "ZZ"
703 -> "AAA"
0 -> ""
2147483647 -> "FXSHRXW"
Look at 26 -> "Z"27 -> "AA"
The line 0 -> ""int
Complexity
Each pass of the loop produces one letter of the title, and one reverse puts the letters in order.
- Time: O(log26 n) for column number n, because each pass adds one letter and divides
by 26, so a title of seven letters, the longest ancolumnNumber gives, takes seven passes.int - Space: O(log26 n) for the title, because
and the returned string each hold one character per letter of the title.sb
The reverse conversion, from a title back to its number, is