171. Excel Sheet Column Number
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to convert an Excel column title, given as a string (e.g., "A", "B", "AA", "AB"), into its corresponding column number. This can be viewed as a number system conversion problem. Excel columns follow a base-26 numbering system. However, it's slightly different from a standard base-26 system. In a standard system, digits would range from 0 to 25. Here, the "digits" are the letters 'A' through 'Z', representing the numbers 1 through 26.
Solution
Excel Sheet Column Number in Python
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for char in columnTitle:
# Calculate the value of the character (A=1, B=2, ..., Z=26)
# ord(char) gets the ASCII value of the character.
d = ord(char) - ord('A') + 1
# This process is equivalent to converting a base-26 number to decimal.
# For each new digit (character), we multiply the current total by the base (26)
# and add the value of the new digit.
result = result * 26 + d
return result
Excel Sheet Column Number in Java
class Solution {
public int titleToNumber(String columnTitle) {
// Use long to prevent potential overflow during intermediate calculations.
// The problem constraints guarantee the final result fits in a 32-bit int.
long result = 0;
for (int i = 0; i < columnTitle.length(); i++) {
char c = columnTitle.charAt(i);
// Calculate the value of the character: 'A' -> 1, 'B' -> 2, ..., 'Z' -> 26.
int d = c - 'A' + 1;
// Treat the column title as a base-26 number.
// For each new character, we multiply the existing result by 26
// and add the value of the current character.
result = result * 26 + d;
}
// The final result is guaranteed to fit in an int per the problem constraints.
return (int) result;
}
}
Excel Sheet Column Number in C++
class Solution {
public:
int titleToNumber(string columnTitle) {
// Use long long to be safe against overflow, although the problem constraints
// guarantee the final result fits within a 32-bit signed integer.
long long result = 0;
for (char c : columnTitle) {
// Calculate the value of the character (A=1, B=2, ..., Z=26).
int d = c - 'A' + 1;
// This is like converting a base-26 number to base-10.
// For each new character, we multiply the existing result by 26
// (shifting it to the next higher power) and add the new character's value.
result = result * 26 + d;
}
return (int)result;
}
};
Excel Sheet Column Number in TypeScript
function titleToNumber(columnTitle: string): number {
let result = 0;
for (let i = 0; i < columnTitle.length; i++) {
// Get the character at the current position.
const char = columnTitle[i];
// Calculate the value of the character: 'A' -> 1, 'B' -> 2, etc.
// charCodeAt(0) gets the UTF-16 code unit value.
const d = char.charCodeAt(0) - 'A'.charCodeAt(0) + 1;
// This is essentially a base-26 to base-10 conversion.
// Multiply the accumulated result by 26 and add the value of the current character.
result = result * 26 + d;
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.