Excel Sheet Column Number
EASYDescription
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 <= 7columnTitleconsists only of uppercase English letters.columnTitleis in the range["A", "FXSHRXW"].
Approaches
Checkout 2 different approaches to solve Excel Sheet Column Number. Click on different approaches to view the approach and algorithm in detail.
Recursive Approach
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.
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 Analysis
Pros and Cons
- Provides a clear, declarative solution that directly models the mathematical recurrence relation.
- 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.
Code Solutions
Checking out 4 solutions in different languages for Excel Sheet Column Number. Click on different languages to view the code.
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 Solution
Watch the video walkthrough for Excel Sheet Column Number
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.