Show problems as
Classic problem #171EasyMathString

Excel Sheet Column Number

Real-world version: The Position of a Shelf Label
Tracked in this browser only
Read Excel Sheet Column Number on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Shelves are labelled A, B, on to Z, then AA, AB and onwards, so the labels run through every string of uppercase letters, shorter labels first and alphabetically within a length.

Return the position of the shelf labelled label, counting from 1.

Examples

Example 1

Input
label = "Z"
Output
26

The last of the single-letter labels sits at position 26.

Example 2

Input
label = "AA"
Output
27

After the twenty-six single letters come the two-letter labels, and this is the first of them.

Example 3

Input
label = "XYZ"
Output
16900

Reading the three letters as a base twenty-six number with the first letter worth one gives 16900.

Constraints

  • 1 <= label.length <= 7
  • label holds only uppercase English letters
  • label lies between A and FXSHRXW

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def title_to_number(label: str) -> int:
Java
public int titleToNumber(String label)