253. Sheet Column Number
The export module of a reporting tool receives column names written the way spreadsheets write them: A to Z for the first 26 columns, then AA, AB, ... AZ, BA, ... ZZ, AAA, and so on. It needs the 1-based position of a column to index into an array.
Given the upper-case string label, return the column number it denotes. For example A is 1, Z is 26 and AA is 27. The label is always valid and its position fits in a 32-bit signed integer. Your solution should read the label once.
Example 1
- Input:
- label = "KQ"
- Output:
- 303
- Explanation:
K is 11 and Q is 17, so the value is 11 * 26 + 17 = 303.
Example 2
- Input:
- label = "ZZZ"
- Output:
- 18278
- Explanation:
ZZZ is 26*676 + 26*26 + 26 = 18278, the last three-letter column.
Example 3
- Input:
- label = "BAD"
- Output:
- 1382
- Explanation:
B=2, A=1, D=4 gives (2*26 + 1) * 26 + 4 = 1382.
Constraints
1 ≤ label.length ≤ 7
label contains only the characters A-Z.
The denoted column number is at most 231 - 1 (the largest label is FXSHRXW).
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(L)
- Space
- O(1)