168. Excel Sheet Column Title - Explanation
Description
You are 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 = 32
Output: "AF"Example 2:
Input: columnNumber = 53
Output: "BA"Constraints:
1 <= columnNumber <= ((2^31)-1)
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Number Base Conversion - Converting numbers between different bases (especially base-26)
- Modular Arithmetic - Using modulo and integer division to extract digits
- Recursion - Breaking down problems into smaller subproblems with base cases
- ASCII/Character Manipulation - Converting between characters and their numeric values
1. Recursion
Intuition
Excel columns use a bijective base-26 system where A=1, B=2, ..., Z=26. Unlike standard base conversion where digits range from 0 to base-1, here digits range from 1 to 26. This means we need to adjust by subtracting 1 before finding each digit.
After subtracting 1, we can use modulo 26 to find the rightmost character and divide by 26 to get the remaining prefix. Recursion handles this naturally: first solve for the prefix (if any), then append the current character.
Algorithm
- Base case: if
columnNumberis0, return an empty string. - Subtract
1fromcolumnNumberto convert to 0-indexed. - Recursively call for
n // 26to get the prefix string. - Compute the current character as
chr('A' + n % 26). - Return the prefix concatenated with the current character.
Time & Space Complexity
- Time complexity:
- Space complexity: for recursion stack.
Where is the given column number.
2. Iteration
Intuition
The iterative approach works from right to left, building the result string in reverse. At each step, we extract the rightmost character, then reduce the number for the next iteration. Since we build characters from least significant to most significant, we reverse the result at the end.
This avoids recursion overhead and makes the process explicit: subtract 1, find the character via modulo, divide by 26, and repeat until the number becomes 0.
Algorithm
- Initialize an empty list
resto collect characters. - While
columnNumber > 0:- Decrement
columnNumberby1. - Compute the offset as
columnNumber % 26. - Append the character
chr('A' + offset)tores. - Divide
columnNumberby26.
- Decrement
- Reverse
resand join to form the final string.
Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
Where is the given column number.
Common Pitfalls
Treating It as Standard Base-26 Conversion
Unlike standard base conversion where digits range from 0 to 25, Excel columns use a 1-indexed system where A=1 and Z=26. Forgetting to subtract 1 before taking modulo will cause off-by-one errors. For example, column 26 should be "Z", but without the subtraction, you would incorrectly compute the character for position 0.
Forgetting to Reverse the Result in Iterative Approach
When building the string iteratively by repeatedly taking modulo and dividing, you generate characters from least significant (rightmost) to most significant (leftmost). If you forget to reverse the accumulated characters at the end, "ABC" would incorrectly become "CBA". Either build the string in reverse or explicitly reverse it before returning.
Sign in to join the discussion