166. Excel Sheet Column Title
Spreadsheet columns are labeled with letters instead of numbers: the first column is A, the 26th is Z, the 27th starts over as AA, then AB, and so on — after AZ comes BA, and after ZZ comes AAA.
Given a positive integer columnNumber, return the corresponding column label as a string of uppercase letters.
This looks like plain base-26 conversion, but it is not quite: there is no zero digit. Every place reads A through Z meaning 1 through 26, which is why 26 is Z rather than rolling over to a two-letter label. Getting that one detail right is the entire problem.
Example 1:
Input: columnNumber = 1
Output: "A"
Explanation: Column 1 is the very first label, A.
Example 2:
Input: columnNumber = 28
Output: "AB"
Explanation: After Z (26) come AA (27) and AB (28).
Example 3:
Input: columnNumber = 701
Output: "ZY"
Explanation: 701 = 26 * 26 + 25, which reads Z (26) in the high place and Y (25) in the low place.
Constraints:
- 1 ≤ columnNumber ≤ 2³¹ - 1
Hints:
Try ordinary base-26 thinking first and watch where it breaks: 26 should be Z, but 26 % 26 = 0 and there is no letter for 0.
The digits run 1..26 instead of 0..25 (a 'bijective' numeral system). Subtracting 1 from the number before each % 26 / ÷ 26 step shifts the range back to 0..25 so it maps cleanly onto 'A'..'Z'.
Each step produces the lowest-order letter, so either build the string and reverse it at the end, or let recursion emit the high-order letters first.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: columnNumber = 1
Expected output: "A"