All problems
1157EasyMathString

The Position of a Shelf Label

Tracked in this browser only
Write code

Trains the technique from

LeetCode 171Excel Sheet Column Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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