Excel Sheet Column Title

Easy
158.

Excel Sheet Column Title

Excel Sheet Column Title is an easy coding problem on strings and math, asked in Amazon, Microsoft, Meta and Zoho interviews.

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

1  -> A
2  -> B
…
26 -> Z
27 -> AA
28 -> AB
…

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
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} columnNumber
 * @return {string}
 */
var convertToTitle = function(columnNumber) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Excel Sheet Column Title — Solution in Python, Java & C++

Excel Sheet Column Title solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Excel Sheet Column Title is an easy coding problem on strings and math, asked in Amazon, Microsoft, Meta and Zoho interviews.

  • Difficulty: Easy
  • Topics: Strings, Math
  • Asked at: Amazon, Microsoft, Meta, Zoho, Ola, Directi
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

1  -> A
2  -> B
…
26 -> Z
27 -> AA
28 -> AB
…

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

Hints for Excel Sheet Column Title

  1. Hint 1

    This is base 26 with digits 1…26 instead of 0…25, so the usual % 26 is off by one.

  2. Hint 2

    Decrement the number by 1 before each digit extraction; that shifts the range back to 0…25.

  3. Hint 3

    Build the string from the least significant letter, prepending as you go.

How to solve Excel Sheet Column Title

Convert to bijective base 26. Decrementing by one before each digit extraction shifts the 1…26 digit range back to 0…25 so that ordinary % 26 and // 26 work.

Approach

  1. While the number is positive: subtract 1, append the letter 'A' + n % 26, then divide by 26.
  2. Reverse the collected letters and join.

Why it works

In a bijective numeration there is no zero digit, so the usual last step of n % 26 == 0 cannot mean "empty". Subtracting one maps the digit 26 (Z) to remainder 25 and makes the quotient the correct prefix, turning the problem into a plain base conversion.

Complexity

  • Time — O(log₂₆ n)
  • Space — O(log₂₆ n)

Pitfalls

  • Forgetting the decrement turns 26 into A0-style garbage instead of Z, and 52 into B0 instead of AZ.
  • Letters come out least significant first; build a list and reverse it rather than repeatedly prepending.

Reference solution

Python

def convertToTitle(columnNumber: int) -> str:
    out = []
    while columnNumber > 0:
        columnNumber -= 1
        out.append(chr(ord('A') + columnNumber % 26))
        columnNumber //= 26
    return "".join(reversed(out))

Java

public static String convertToTitle(int columnNumber) {
    StringBuilder sb = new StringBuilder();
    while (columnNumber > 0) {
        columnNumber--;
        sb.append((char) ('A' + columnNumber % 26));
        columnNumber /= 26;
    }
    return sb.reverse().toString();
}

C++

string convertToTitle(int columnNumber) {
    string out = "";
    while (columnNumber > 0) {
        columnNumber--;
        out += (char) ('A' + columnNumber % 26);
        columnNumber /= 26;
    }
    reverse(out.begin(), out.end());
    return out;
}

JavaScript

var convertToTitle = function(columnNumber) {
    let out = "";
    while (columnNumber > 0) {
        columnNumber--;
        out = String.fromCharCode(65 + (columnNumber % 26)) + out;
        columnNumber = Math.floor(columnNumber / 26);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings