Classic problem #171EasyMathString
Excel Sheet Column Number
Real-world version: The Position of a Shelf LabelTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's