Excel Sheet Column Number
Excel Sheet Column Number is a easy arrays & hashing problem solved with the math / number theory pattern.
The best approach, optimal (left to right, horner's rule), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (right to left with place values) up.
Problem
Spreadsheet columns are labelled A, B, …, Z, then AA, AB, …, AZ, BA, and so on. Column A is number 1, Z is 26, AA is 27, and the pattern continues with longer titles.
Given a column title columnTitle made of uppercase letters, return its column number.
Examples
Example 1
- Input
columnTitle = "C"- Output
3- Why
- Single letters map A -> 1, B -> 2, C -> 3.
Example 2
- Input
columnTitle = "BA"- Output
53- Why
- B is worth 2 full rounds of 26 columns, then A adds 1: 2 * 26 + 1 = 53.
Example 3
- Input
columnTitle = "AAA"- Output
703- Why
- 1 * 26² + 1 * 26 + 1 = 676 + 26 + 1 = 703.
Constraints
1 <= columnTitle.length <= 7columnTitlecontains only uppercase English letterscolumnTitleis between"A"and"FXSHRXW"(the column number fits in a 32-bit signed integer)
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (right to left with place values) | O(n) | O(1) |
| Optimal (left to right, Horner's rule) | O(n) | O(1) |
1Brute force (right to left with place values)
O(n)One pass over the n letters.O(1)Only the result and the place value.Treat the title as a number in base 26 whose digits are 1..26. Walk from the last letter to the first, multiplying each letter value by its place value 1, 26, 26², … and summing.
- result = 0, place = 1.
- For i from the last index down to 0: result += (title[i] - 'A' + 1) * place; place *= 26.
- Return result.
class Solution {
public int titleToNumber(String columnTitle) {
long result = 0, place = 1;
for (int i = columnTitle.length() - 1; i >= 0; i--) {
result += (columnTitle.charAt(i) - 'A' + 1) * place;
place *= 26;
}
return (int) result;
}
}class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result, place = 0, 1
for ch in reversed(columnTitle):
result += (ord(ch) - ord("A") + 1) * place
place *= 26
return resultclass Solution {
public:
int titleToNumber(string columnTitle) {
long long result = 0, place = 1;
for (int i = (int)columnTitle.size() - 1; i >= 0; i--) {
result += (columnTitle[i] - 'A' + 1) * place;
place *= 26;
}
return (int)result;
}
};function titleToNumber(columnTitle) {
let result = 0, place = 1;
for (let i = columnTitle.length - 1; i >= 0; i--) {
result += (columnTitle.charCodeAt(i) - 64) * place;
place *= 26;
}
return result;
}2Optimal (left to right, Horner's rule)
O(n)One multiply and one add per letter.O(1)Only the running value.Read the letters from left to right. Each time a new letter arrives, everything read so far moves up one base-26 place, so multiply the running value by 26 and add the new letter's value. No separate place value is needed.
- result = 0.
- For each letter c: result = result * 26 + (c - 'A' + 1).
- Return result.
class Solution {
public int titleToNumber(String columnTitle) {
int result = 0;
for (int i = 0; i < columnTitle.length(); i++) {
result = result * 26 + (columnTitle.charAt(i) - 'A' + 1);
}
return result;
}
}class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for ch in columnTitle:
result = result * 26 + (ord(ch) - ord("A") + 1)
return resultclass Solution {
public:
int titleToNumber(string columnTitle) {
int result = 0;
for (char c : columnTitle) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
};function titleToNumber(columnTitle) {
let result = 0;
for (let i = 0; i < columnTitle.length; i++) {
result = result * 26 + (columnTitle.charCodeAt(i) - 64);
}
return result;
}Edge cases to test
- A single letter, including Z = 26
- Titles ending in Z, such as AZ = 52, where there is no digit 0
- The largest title FXSHRXW = 2147483647
Hints
Hint 1
This is like reading a base-26 number, except the digits run from 1 (A) to 26 (Z) instead of 0 to 25.
Hint 2
Reading left to right, every new letter shifts the value so far one place up: result = result * 26 + value(letter).
FAQ
What is the best time complexity for Excel Sheet Column Number?
Optimal (left to right, Horner's rule) runs in O(n) time and O(1) extra space. One multiply and one add per letter.
Which pattern does Excel Sheet Column Number use?
It is a arrays & hashing problem that uses the math / number theory pattern. Other problems with the same pattern: Bulb Switcher, Reaching Points, Water and Jug Problem.
Is there a brute force solution for Excel Sheet Column Number?
Yes. Brute force (right to left with place values) takes O(n) time and O(1) space. Treat the title as a number in base 26 whose digits are 1..26.
Which edge cases should I test for Excel Sheet Column Number?
A single letter, including Z = 26; Titles ending in Z, such as AZ = 52, where there is no digit 0; The largest title FXSHRXW = 2147483647.