Excel Sheet Column Number - Problem

Ever wondered how Excel magically converts column titles like A, B, AA, ZY into numbers behind the scenes? Here's your chance to decode the mystery!

Given a string columnTitle that represents the column title as it appears in an Excel sheet, return its corresponding column number.

Think of it as a base-26 number system where:

  • A → 1
  • B → 2
  • C → 3
  • ...
  • Z → 26
  • AA → 27
  • AB → 28
  • ...

Your task is to convert any valid Excel column title into its numeric equivalent.

Input & Output

example_1.py — Single Character
$ Input: columnTitle = "A"
Output: 1
💡 Note: A is the first column, so it corresponds to 1
example_2.py — Two Characters
$ Input: columnTitle = "AB"
Output: 28
💡 Note: AB = A×26 + B = 1×26 + 2 = 28. After Z(26), we get AA(27), then AB(28)
example_3.py — Boundary Case
$ Input: columnTitle = "ZY"
Output: 701
💡 Note: ZY = Z×26 + Y = 26×26 + 25 = 676 + 25 = 701

Constraints

  • 1 ≤ columnTitle.length ≤ 7
  • columnTitle consists only of uppercase English letters
  • columnTitle is in the range ["A", "FXSHRXW"]

Visualization

Tap to expand
Excel Column Number ConversionSingle Letters (Base Case):A1B2...Z26Two Letters (AB = 28):ABStep 1: result = 0Step 2: Process A → result = 0×26 + 1 = 1Step 3: Process B → result = 1×26 + 2 = 28Answer: 28Pattern Recognition:After Z (26), comes AA (27)After AZ (52), comes BA (53)After ZZ (702), comes AAA (703)Formula:result = result × 26 + char_valuewhere char_value = char - 'A' + 1Three Letters Example (ABC = 731):ABCA: result = 0×26 + 1 = 1B: result = 1×26 + 2 = 28C: result = 28×26 + 3 = 731
Understanding the Visualization
1
Understand the Pattern
A=1, B=2, ..., Z=26, AA=27, AB=28, ...
2
Apply Base-26 Logic
Each position represents a power of 26
3
Calculate from Left to Right
Multiply by 26 and add current character value
4
Get Final Result
The accumulated value is our answer
Key Takeaway
🎯 Key Insight: Excel columns use a bijective base-26 system where each position represents powers of 26, but with 1-based indexing instead of 0-based.
Asked in
Microsoft 45 Google 32 Amazon 28 Meta 15
89.5K Views
Medium Frequency
~12 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen