Watch 10 video solutions for Excel Sheet Column Number, a easy level problem involving Math, String. This walkthrough by Knowledge Center has 32,550 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 ...
Example 1:
Input: columnTitle = "A" Output: 1
Example 2:
Input: columnTitle = "AB" Output: 28
Example 3:
Input: columnTitle = "ZY" Output: 701
Constraints:
1 <= columnTitle.length <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].Problem Overview: Excel labels columns using letters: A, B, ..., Z, AA, AB, and so on. Given a column title string, convert it to its corresponding numeric index. The mapping behaves like a modified base‑26 number system where A=1, B=2, ..., Z=26.
Approach 1: Iterative Approach Using Base Conversion (O(n) time, O(1) space)
This problem is essentially base conversion. Treat the column title as a number written in base 26, but with digits ranging from 1 to 26 instead of 0 to 25. Iterate through each character from left to right. For every character, multiply the current result by 26 and add the value of the current letter (char - 'A' + 1). This mirrors how decimal numbers are built digit by digit: result = result * 26 + value. The algorithm performs a single pass through the string, making it efficient and easy to implement in any language. Since only a running integer is maintained, the space usage stays constant. This approach relies on simple arithmetic and character manipulation from math and string fundamentals.
Example: for "AB", start with result = 0. Process 'A': 0 * 26 + 1 = 1. Process 'B': 1 * 26 + 2 = 28. The final result is 28. The same logic scales for longer titles like "FXSHRXW" without any additional data structures.
Approach 2: Recursive Approach (O(n) time, O(n) recursion space)
The same base‑26 logic can be expressed recursively. Process the string from left to right by splitting it into two parts: the prefix and the last character. Recursively compute the numeric value of the prefix, multiply it by 26, and then add the value of the final character. Each recursive call reduces the string size by one until the base case (single character) is reached. The formula becomes: value(prefix) * 26 + value(last_char).
This version mirrors how positional number systems work conceptually, making it useful for explaining the idea in interviews or teaching recursion. However, it introduces call stack overhead proportional to the string length, so its auxiliary space complexity becomes O(n). For production or competitive coding, the iterative version is typically preferred because it avoids recursion overhead while producing the same result.
Recommended for interviews: The iterative base‑conversion approach is what interviewers expect. It demonstrates that you recognize the Excel column naming scheme as a positional base‑26 system and can translate that insight into a simple loop. Mentioning the recursive formulation shows deeper understanding, but implementing the iterative solution quickly and correctly is usually the strongest signal.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Base Conversion | O(n) | O(1) | Best general solution; minimal memory and simplest implementation |
| Recursive Approach | O(n) | O(n) | Useful for explaining positional base‑26 logic recursively |