171. Excel Sheet Column Number
easyConvert 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 Sam K., 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 <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].
Examples
Example 1
columnTitle = "A"1Example 2
columnTitle = "AB"28Example 3
columnTitle = "ZY"701Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
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
More Math practice problems
- 7. Reverse Integer
- 8. String to Integer (atoi)
- 9. Palindrome Number
- 12. Integer to Roman
- 13. Roman to Integer
- 29. Divide Two Integers
- 38. Count and Say
- 43. Multiply Strings