Excel Sheet Column Title
The key idea
This is base-26 but 1-indexed (
A = 1, not 0), so there is no digit for zero. Subtract 1 before each % 26 and // 26 to shift into a normal 0-25 range; that single offset is what makes Z (26) map to the last letter of one place instead of overflowing into a new place.Problem
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
The columns are labeled like the headers of a spreadsheet: A is 1, B is 2, on up to Z which is 26. The very next column wraps to AA (27), then AB (28), and so on — exactly like counting, but with 26 letters as digits instead of 10.
The catch is that this numbering is 1-indexed: there is no symbol for zero, so it is *not* ordinary base-26. A stands for 1 rather than 0, which means each place must be nudged by 1 before you can treat it as a normal base-26 digit.
Constraints
1 <= columnNumber <= 2^31 - 1
Examples
Input: columnNumber = 1
Output: "A"
Input: columnNumber = 28
Output: "AB"
Input: columnNumber = 701
Output: "ZY"
Complexity
Time: O(log n) Space: O(log n)
See the full solution
- ✓Full worked approach
- ✓Reference code in 5 languages
- ✓Problem-solving tips
- ✓Step-by-step animated visualization
More Math / Number Theory problems
- Add BinaryEASY
- Factorial Trailing ZeroesMEDIUM
- Greatest Common Divisor of StringsEASY
- Integer to RomanMEDIUM
- Multiply StringsMEDIUM
- Palindrome NumberEASY
- Plus OneEASY
- Reverse IntegerMEDIUM
- Roman to IntegerEASY