Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: columnNumber = 1 Output: "A"
Example 2:
Input: columnNumber = 28 Output: "AB"
Example 3:
Input: columnNumber = 701 Output: "ZY"
Constraints:
1 <= columnNumber <= 231 - 1The #168 Excel Sheet Column Title problem asks you to convert a positive integer into its corresponding Excel column title (e.g., 1 → A, 26 → Z, 27 → AA). The key idea is recognizing that Excel columns follow a base-26 representation, but unlike a typical base-26 system, it is 1-indexed instead of 0-indexed. This subtle difference means you must carefully adjust the number before mapping values to characters.
A common strategy is to repeatedly determine the character corresponding to the last "digit" of the column representation and build the result string step by step. Each step maps a value from 1–26 to letters A through Z. After extracting a character, the number is reduced to continue processing the remaining digits. Because the number shrinks by a factor of 26 each iteration, the algorithm runs efficiently even for large inputs.
This approach primarily uses simple math operations and string construction, resulting in a time complexity proportional to the number of base‑26 digits in the input.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Base-26 Conversion with Character Mapping | O(log26 n) | O(log26 n) |
Greg Hogg
To solve this problem, we use a mathematical approach similar to converting a number to a different base. Here, the base is 26, similar to the English alphabet, where each letter corresponds to a unique representation of a number. This approach involves repeated division by 26 and mapping the remainder to the corresponding letter in the alphabet.
Time Complexity: O(log26(n)), where n is the columnNumber, due to repeated division by 26.
Space Complexity: O(log26(n)), to store the result string.
1function convertToTitle(columnNumber) {
2 let result = '';
3 while (columnNumber > 0) {
4 columnNumber--;
5
This JavaScript function builds the result string by prepending characters, which are derived from the mapped remainder using String.fromCharCode.
This approach uses recursion to solve the problem. The function calls itself with the quotient of the column number divided by 26, constructing the result string backwards using the modulo operation to index into the alphabet.
Time Complexity: O(log26(n))
Space Complexity: O(log26(n)), due to the recursive call stack.
1
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this problem or variations of it frequently appear in coding interviews, including at top tech companies. It tests understanding of number systems, edge cases in conversions, and careful handling of indexing.
The problem mainly uses string manipulation along with basic arithmetic operations. A string or character builder is typically used to append letters as each base-26 digit is determined.
The optimal approach treats the column number like a modified base-26 number system. By repeatedly mapping values from 1–26 to letters A–Z and reducing the number each step, you can build the column title efficiently. This method runs in O(log26 n) time.
In standard base-26 systems digits range from 0 to 25, but Excel columns use letters A–Z representing 1 to 26. Because of this 1-based indexing, a small adjustment is needed before extracting each character during conversion.
The Java solution provides a recursive function that continuously reduces the column number, processing each 'digit', and appends characters to the result string as the recursion unwinds.