Excel Sheet Column Number
EasyPrompt
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: 1Example 2:
Input: columnTitle = "AB"
Output: 28Example 3:
Input: columnTitle = "ZY"
Output: 701
Constraints:
1 <= columnTitle.length <= 7columnTitleconsists only of uppercase English letters.columnTitleis in the range["A", "FXSHRXW"].
Approaches
2 approaches with complexity analysis and trade-offs.
This method solves the problem by defining it in terms of itself. The column number for a title like "ABC" can be seen as the column number for "AB" multiplied by 26, plus the value of 'C'. This forms a recursive relationship that can be implemented with a function that calls itself on a smaller version of the input string.
Algorithm
- Define a function
titleToNumber(columnTitle). - Base Case: If
columnTitleis empty, return 0. - Recursive Step:
- a. Get the prefix of the string (all characters except the last).
- b. Get the value of the last character (
lastChar - 'A' + 1). - c. Return
titleToNumber(prefix) * 26 + value_of_last_char.
Walkthrough
The core idea is to break down the problem. For a given columnTitle, we can separate the last character from the rest of the string (the prefix). The final number is 26 * (number for prefix) + (value of last character).
- The base case for the recursion is an empty string, which corresponds to the number 0.
- For any non-empty string, the function recursively calls itself with the prefix (the string without its last character) and uses the result to compute the final number.
- For example,
titleToNumber("ZY")would be calculated astitleToNumber("Z") * 26 + 25. The call totitleToNumber("Z")would in turn betitleToNumber("") * 26 + 26. SincetitleToNumber("")is 0, the result unfolds back to(0 * 26 + 26) * 26 + 25 = 701.
class Solution { public int titleToNumber(String columnTitle) { // Base case: if the string is empty, its value is 0. if (columnTitle == null || columnTitle.isEmpty()) { return 0; } // Recursive step: // Get the prefix (all but the last character) String prefix = columnTitle.substring(0, columnTitle.length() - 1); // Get the last character char lastChar = columnTitle.charAt(columnTitle.length() - 1); // The value of the last character (A=1, B=2, ...) int lastCharValue = lastChar - 'A' + 1; // The recursive formula return titleToNumber(prefix) * 26 + lastCharValue; }}Complexity
Time
O(N^2)
Space
O(N^2)
Trade-offs
Pros
Provides a clear, declarative solution that directly models the mathematical recurrence relation.
Cons
Inefficient due to the overhead of recursive calls and repeated string manipulations (like
substring).Can lead to a
StackOverflowErrorfor very long strings, although not an issue with the problem's constraints.Higher space complexity due to the recursion stack.
Solutions
Solution
public class Solution { public int TitleToNumber(string columnTitle) { int ans = 0; foreach(char c in columnTitle) { ans = ans * 26 + c - 'A' + 1; } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.