Excel Sheet Column Number - Problem

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

In Excel, columns are labeled as:

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

This is essentially converting from base-26 numbering system to decimal, where A=1, B=2, ..., Z=26.

Input & Output

Example 1 — Single Character
$ Input: columnTitle = "A"
Output: 1
💡 Note: A is the first column, so it corresponds to column number 1
Example 2 — Two Characters
$ Input: columnTitle = "AB"
Output: 28
💡 Note: A=1, B=2. Result = 1×26 + 2 = 28. This is column AB which comes after Z (26) and AA (27)
Example 3 — Last Single Character
$ Input: columnTitle = "Z"
Output: 26
💡 Note: Z is the 26th letter, corresponding to column number 26

Constraints

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

Visualization

Tap to expand
Excel Sheet Column Number INPUT A B C ... 1 2 3 columnTitle: "A" Base-26 System: A=1, B=2, ..., Z=26 AA=27, AB=28, ... ASCII: 'A' = 65 Value = 65 - 64 = 1 ALGORITHM STEPS 1 Initialize result = 0 2 Loop Each Char Process left to right 3 Calculate Value char_val = char - 'A' + 1 4 Update Result result = result*26 + val For "A": result = 0 char 'A': val = 65-64 = 1 result = 0*26 + 1 = 1 FINAL RESULT Column "A" maps to: 1 Output: 1 Verification Excel Column A = 1 OK Key Insight: This is a base-26 to decimal conversion. Each position has weight 26^(n-1-i). Formula: result = result * 26 + (char - 'A' + 1). Time: O(n), Space: O(1). TutorialsPoint - Excel Sheet Column Number | Optimal Solution
Asked in
Microsoft 45 Google 30 Amazon 25
180.0K Views
Medium Frequency
~15 min Avg. Time
2.1K 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