Skip to problem
←

Excel Sheet Column Title

Easy 0 attempts

Spreadsheet programs like Excel name columns A, B, …, Z, then continue with AA, AB, …, then AAA, and so forth. Given a positive integer columnNumber — where 1 corresponds to A, 2 to B, …, 26 to Z, and 27 to AA — return the matching column title as a string.

Think of it as base-26, except the digits start counting from 1 instead of 0, unlike ordinary binary or decimal place-value systems.

Function contract

columnNumber is a positive, one-based spreadsheet column index.

Output: Return its uppercase column title, using A–Z.

Example 1

Input
columnNumber = 1
Output
"A"

Example 2

Input
columnNumber = 28
Output
"AB"

Example 3

Input
columnNumber = 701
Output
"ZY"
Constraints
  • 1 <= columnNumber <= 2^31 - 1
Topics

One-Based Base-26 Conversion

Subtract one before extracting each digit, mapping the remainders 0–25 to A–Z. Collect letters from right to left, then reverse once.

javascript
function convertToTitle(columnNumber) {
  const letters = [];
  while (columnNumber > 0) {
    columnNumber--;
    letters.push(String.fromCharCode(65 + (columnNumber % 26)));
    columnNumber = Math.floor(columnNumber / 26);
  }
  return letters.reverse().join('');
}

Time: O(log n) | Space: O(log n), including the output string

Loading discussion…

Saved in this browser only. Private to you.

JavaScript