Excel Sheet Column Number

Easy Arrays & Hashing Math / Number Theory Original on LeetCode

Reported atRazorpay

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 <= 7
  • columnTitle contains only uppercase English letters
  • columnTitle is 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.

00:00
Testcase

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.

ApproachTimeSpace
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)

TimeO(n)One pass over the n letters.
SpaceO(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.

  1. result = 0, place = 1.
  2. For i from the last index down to 0: result += (title[i] - 'A' + 1) * place; place *= 26.
  3. 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;
    }
}

2Optimal (left to right, Horner's rule)

TimeO(n)One multiply and one add per letter.
SpaceO(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.

  1. result = 0.
  2. For each letter c: result = result * 26 + (c - 'A' + 1).
  3. 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;
    }
}

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.