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 - 1Problem Overview: You’re given a positive integer representing a column number in Excel. The task is to convert it into its corresponding Excel column title. For example, 1 -> A, 26 -> Z, 27 -> AA, and 701 -> ZY. The tricky part is that Excel columns use a 1-indexed alphabet system rather than a typical 0-indexed base system.
Approach 1: Base 26 Conversion (O(log₍26₎ n) time, O(1) space)
This problem behaves like converting a number into base 26, but with a twist: Excel columns are 1-based instead of 0-based. That means A = 1 instead of 0. To handle this, subtract 1 from the number before taking the modulo. Repeatedly compute (n - 1) % 26 to determine the current character, convert it to a letter using ASCII offset ('A' + remainder), and divide the number by 26 using integer division. Continue until the number becomes zero, building the string in reverse order. This approach uses simple arithmetic and math operations along with basic string construction. Since each step divides the number by 26, the number of iterations is proportional to the number of base‑26 digits, giving O(log₍26₎ n) time complexity and constant extra space.
Approach 2: Recursive Solution (O(log₍26₎ n) time, O(log₍26₎ n) space)
The recursive version applies the same base‑26 insight but expresses the process through function calls. If the column number is zero, return an empty string. Otherwise, decrement the number by one, compute the character for (n % 26), and recursively process n / 26. Each recursive call resolves one character of the final column title, building the result from left to right when the stack unwinds. The algorithm still performs one step per base‑26 digit, so the time complexity remains O(log₍26₎ n). However, recursion adds call stack usage proportional to the number of digits, giving O(log₍26₎ n) space complexity.
Recommended for interviews: The iterative base‑26 conversion is the expected answer in most interviews. It demonstrates that you understand how Excel’s column numbering differs from standard base systems and how to handle the off‑by‑one adjustment. Mentioning the recursive alternative shows deeper understanding, but the iterative solution is cleaner and avoids stack overhead. Interviewers usually look for the (n - 1) % 26 insight and the repeated division by 26.
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.
The function convertToTitle converts an integer to a column title by repeatedly dividing the number by 26 and using the remainder, adjusted by one, to map to a character. The result is constructed in reverse order, so it needs to be reversed before returning.
C++
Java
Python
C#
JavaScript
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.
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.
The recursive C function processes the column number by passing it with each reduced state until the base case is reached, building the result backwards by appending characters to the result as the recursive stack unwinds.
C++
Java
Python
C#
JavaScript
Time Complexity: O(log26(n))
Space Complexity: O(log26(n)), due to the recursive call stack.
| Approach | Complexity |
|---|---|
| Base 26 Conversion | Time Complexity: O(log26(n)), where n is the columnNumber, due to repeated division by 26. |
| Recursive Solution | Time Complexity: O(log26(n)) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Base 26 Conversion (Iterative) | O(log₍26₎ n) | O(1) | Best general solution. Efficient and avoids recursion overhead. |
| Recursive Conversion | O(log₍26₎ n) | O(log₍26₎ n) | Useful for demonstrating recursive decomposition of base conversion. |
Microsoft Coding Interview Question! | Excel Sheet Column Number - Leetcode 171 • Greg Hogg • 79,092 views views
Watch 9 more video solutions →Practice Excel Sheet Column Title with our built-in code editor and test cases.
Practice on FleetCode