Excel Sheet Column Title - Solution & Explanation
Problem Statement
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 - 1
Approach Overview
Problem Overview: Given a positive integer representing a column number in Excel, convert it to the corresponding column title. Excel uses a 1-indexed alphabet system: 1 → A, 2 → B, ..., 26 → Z, 27 → AA, 28 → AB.
Approach 1: Base 26 Conversion (O(log n) time, O(1) space)
This problem behaves like a base-26 number system but with a twist: digits range from 1–26 instead of 0–25. The fix is to decrement the number before each modulo operation. Repeatedly compute (n - 1) % 26 to determine the current character, map it to 'A' + remainder, append it to the result, and divide the number by 26 using integer division. Continue until the number becomes zero, then reverse the constructed string. The algorithm processes one character per iteration, so the runtime is O(log26 n), and only constant auxiliary space is used aside from the output string. This approach relies on simple arithmetic from Math and basic manipulation of a String.
Approach 2: Recursive Solution (O(log n) time, O(log n) space)
The same base‑26 idea can be expressed recursively. If the column number is zero, return an empty string. Otherwise, reduce the problem size by converting (n - 1) / 26 recursively, then append the current character derived from (n - 1) % 26. Each recursive call processes one “digit” in the Excel title representation. The recursion depth equals the number of characters in the final title, which is O(log26 n). Time complexity remains O(log n), but recursion introduces call stack overhead, giving O(log n) space complexity. This version is useful when practicing Recursion patterns or when building the string from left to right naturally.
Recommended for interviews: The iterative base‑26 conversion is the expected solution. It shows you understand the off‑by‑one detail caused by Excel’s 1-indexed alphabet mapping. Interviewers often look for the n-- adjustment before modulo. The recursive version is clean and expressive, but the iterative approach is simpler, avoids stack usage, and demonstrates stronger control over numeric conversions.
Approach 1: Base 26 Conversion
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.
Complexity
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.
Approach 2: Recursive Solution
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.
Complexity
Time Complexity: O(log26(n))
Space Complexity: O(log26(n)), due to the recursive call stack.
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| 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)) |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Base 26 Conversion (Iterative) | O(log n) | O(1) | Best general solution. Efficient and commonly expected in interviews. |
| Recursive Conversion | O(log n) | O(log n) | Useful for practicing recursion or when expressing base conversions recursively. |
Video Solution
Excel Sheet Column Title | LeetCode 168 | C++, Python • Knowledge Center • 34,468 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Excel Sheet Column Title easy or hard?
Excel Sheet Column Title Python/Java solution
How to solve Excel Sheet Column Title in O(log n)?
What is the best approach for Excel Sheet Column Title?
Is Excel Sheet Column Title asked at Google/Amazon/Meta?
What data structure is used in Excel Sheet Column Title?
What is the time complexity of Excel Sheet Column Title?
Ready to solve this problem?
Practice Excel Sheet Column Title with our built-in code editor and test cases.
Practice on FleetCode