Watch 10 video solutions for Excel Sheet Column Title, a easy level problem involving Math, String. This walkthrough by Greg Hogg has 79,092 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |