Skip to main content

Excel Sheet Column Title

Easy
Data Structures and Algorithms
Editorial
Topics:
Math
String
Iteration

A spreadsheet column label is formed from uppercase English letters, with A representing 1, B representing 2, and so on through Z representing 26. After Z, the sequence continues with two-letter labels such as AA, then AB, using the same ordering without a zero-valued digit.

Given the positive integer columnNumber, determine the label of that spreadsheet column. Return the corresponding label as a string.

Implement convertToTitle in the Solution class with the provided signature.

Example 1:

Input: columnNumber = 26
Output: "Z"
Explanation: The 26th column is the last single-letter column, so its label is Z.

Example 2:

Input: columnNumber = 54
Output: "BB"
Explanation: After Z, the labels continue with AA, ..., AZ, then BA, BB; therefore column 54 is BB.

Example 3:

Input: columnNumber = 1000
Output: "ALL"
Explanation: Converting the number using the spreadsheet's one-based letter system produces the three-letter label ALL.

Constraints:

  • 1 <= columnNumber <= 10^5
  • ``columnNumber is an integer.
  • ``columnNumber is positive.
  • The returned label contains only uppercase English letters.

Need help or want more practice?

Practise in an interviewGet AI Coach help