Excel Sheet Column Number
Easy · Math · Math · String
Problem
Spreadsheet columns are labelled A, B, ..., Z, then AA, AB, ..., AZ, BA, ..., ZZ, then AAA, and so on. A is column 1, Z is column 26 and AA is column 27.
Given a label columnTitle, return its column number.
Examples
Input: columnTitle = "C" Output: 3
C is the third letter.
Input: columnTitle = "BD" Output: 56
B is worth 2 · 26 and D adds 4: 56.
Input: columnTitle = "AAA" Output: 703
1 · 676 + 1 · 26 + 1 = 703.
Constraints
- 1 <= len(columnTitle) <= 7
- columnTitle has only uppercase English letters
- columnTitle is at most "FXSHRXW" (column 2^31 - 1)
Approaches
- Baseline: place values from the right — time O(n²), space O(1)
Follow-up
There is no zero digit here: the system is bijective base 26, which is why Z is 26 rather than "10". That matters only for the reverse direction, number to label, where you must subtract 1 before each division. The use cases below count the cells a range covers and read labels over any alphabet.
Related problems
Start free on Cariara · Coding problems · System design studio