Excel Sheet Column Number

easy
math
strings

Spreadsheet columns are labelled A, B, ..., Z, AA, AB, .... Given a column title as a string of uppercase letters, return its corresponding 1-based column number.

So A -> 1, B -> 2, Z -> 26, AA -> 27, AB -> 28, and so on.

Input / output

  • Input: columnTitle: string of uppercase letters AZ
  • Output: the column number as an integer

Examples

  1. columnTitle = "A" returns 1.
  2. columnTitle = "AB" returns 28.
  3. columnTitle = "ZY" returns 701.

Constraints

  • 1 <= columnTitle.length <= 7
  • columnTitle contains only uppercase English letters.
  • The value is in the range [1, 2^31 - 1].

Follow-up This is base-26 with digits 1..26 (there is no zero digit). Why does the absence of a zero digit make it bijective base-26 rather than ordinary base-26?

Examples

Example 1

Input: columnTitle = "A"
Output: 1

Example 2

Input: columnTitle = "AB"
Output: 28

Example 3

Input: columnTitle = "ZY"
Output: 701
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.