252. Sheet Column Label

A reporting tool names the columns of its wide tables like a spreadsheet does: the first 26 columns are A to Z, the next ones continue with AA, AB, ... AZ, BA, ..., ZZ, then AAA, and so on. There is no symbol for zero, so the scheme is not an ordinary base-26 numeral system.

Given the 1-based column position n, return its label as an upper-case string. For example column 26 is Z and column 27 is AA. Your solution should take time proportional to the number of letters in the answer.

Example 1

Input:
n = 52
Output:
"AZ"
Explanation:

Column 52 is the 26th column of the second letter block, so its label is AZ.

Example 2

Input:
n = 18279
Output:
"AAAA"
Explanation:

Columns 1-26 use one letter, 27-702 use two, 703-18278 use three, so 18279 is the first four-letter label AAAA.

Example 3

Input:
n = 12345
Output:
"RFU"
Explanation:

12345 = 18*676 + 6*26 + 21 with digits R=18, F=6, U=21, so the label is RFU.

Constraints

1 ≤ n ≤ 231 - 1

The answer is at most 7 letters long.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(log n)
Space
O(log n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…