168/670

168. Excel Sheet Column Number

Easy

Spreadsheets label their columns with capital letters instead of numbers: column A is 1, B is 2, and so on up to Z at 26. After Z the labels grow to two letters — AA is 27, AB is 28 — and then to three letters, and so on forever.

Given a column label columnTitle (a string of uppercase English letters), return the column's number.

It looks like a quirky puzzle, but it is really just parsing a number written in an unusual base.

Example 1:

Input: columnTitle = "A"

Output: 1

Explanation: The very first column is labeled A, so its number is 1.

Example 2:

Input: columnTitle = "AB"

Output: 28

Explanation: The leading A contributes 1 × 26 = 26 and the B contributes 2, so AB is column 28.

Example 3:

Input: columnTitle = "ZY"

Output: 701

Explanation: Z contributes 26 × 26 = 676 and Y contributes 25, giving 701.

Constraints:

  • 1 ≤ columnTitle.length ≤ 7
  • columnTitle consists only of uppercase English letters A–Z.
  • columnTitle is at most "FXSHRXW", so the answer fits in a signed 32-bit integer.

Hints:

Treat the label as a number written in a strange numeral system. What is the base, and what digit value does each letter carry?

It is base 26 with digits 1..26 (A=1, ..., Z=26) and no zero. Parse it the same way you would parse a decimal string: scan left to right keeping result = result * 26 + value(letter).

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: columnTitle = "A"

Expected output: 1