Skip to main content

171. Excel Sheet Column Number

easy

Convert an Excel column title (A, B, ..., Z, AA, AB, ..., AZ, BA, ...) into its integer column number. A base-26-with-no-zero conversion — a clean demo of why bijective numeral systems matter.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. For example: A -> 1, B -> 2, C -> 3, ..., Z -> 26, AA -> 27, AB -> 28, ...

Constraints

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

Examples

Example 1

Input
columnTitle = "A"
Output
1

Example 2

Input
columnTitle = "AB"
Output
28

Example 3

Input
columnTitle = "ZY"
Output
701

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Think of the column title as a base-26 number with digits A=1, B=2, ..., Z=26. There's no zero digit — this is bijective base-26.

Hint 2

Walk left to right. Maintain a running result; for each char c, result = result * 26 + (c - 'A' + 1).

Hint 3

After 'AB', result = 1 * 26 + 2 = 28. After 'ZY', result = 26 * 26 + 25 = 701.

Hint 4

Equivalently right-to-left: sum (c - 'A' + 1) * 26^position where position counts from 0 at the rightmost char.

Solution approach

Reveal approach

Treat the title as a bijective base-26 number with A=1, ..., Z=26. Walk left to right with result = 0: for each character c, result = result * 26 + (c - 'A' + 1). After processing all characters, return result. O(n) time on the title length (at most 7 here), O(1) space. The bijective representation is what makes this work: in ordinary base-26 you'd have a zero digit and Z would represent both '26' and the leading digit of '10' in some way, leading to ambiguity. Excel skips the zero digit, so the conversion is clean and there are no leading-A issues.

Complexity

Time
O(n)
Space
O(1)

Related patterns

  • math
  • string-scan

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft
  • Apple

Practice these live with InterviewChamp.AI

Drill Excel Sheet Column Number and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →