Excel Sheet Column Number LeetCode Solution

Last updated on January 13th, 2025 at 09:35 pm

Here, we see an Excel Sheet Column Number LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Math

Companies

Microsoft, Uber

Level of Question

Easy

Excel Sheet Column Number LeetCode Solution

Excel Sheet Column Number LeetCode Solution

1. Problem Statement

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

2. Coding Pattern Used in Solution

The coding follows a “Base Conversion” pattern. Specifically, it converts a string representation of a number in a custom base-26 numeral system (where ‘A’ = 1, ‘B’ = 2, …, ‘Z’ = 26) into its corresponding integer value. This is similar to converting a number from one numeral system (e.g., binary, hexadecimal) to decimal.

3. Code Implementation in Different Languages

3.1 Excel Sheet Column Number C++

class Solution {
public:
    int titleToNumber(string columnTitle) {
        int result = 0;
        for(char c : columnTitle)
        {
            int d = c - 'A' + 1;
            result = result * 26 + d;
        }
        return result;
    }
};

3.2 Excel Sheet Column Number Java

public class Solution {
    public int titleToNumber(String columnTitle) {
        if (columnTitle == null) return -1;
        int sum = 0;
        for (char c : columnTitle.toUpperCase().toCharArray()) {
            sum *= 26;
            sum += c - 'A' + 1;
        }
        return sum;
    }
}

3.3 Excel Sheet Column Number JavaScript

var titleToNumber = function(columnTitle) {
    const charCodeBase = 'A'.charCodeAt(0) - 1;
    const n = columnTitle.length;
    let number = 0;
    for (let i = 0; i < n; i++)
        number += (columnTitle.charCodeAt(i) - charCodeBase) * Math.pow(26, n-i-1);
    return number;
};

3.4 Excel Sheet Column Number Python

class Solution(object):
    def titleToNumber(self, columnTitle):
            corresponding_number = 0
            for i in range(len(columnTitle)):
                current_letter=columnTitle[i]
                corresponding_letter_value=ord(current_letter) - ord('A') + 1
                corresponding_number += (corresponding_letter_value *pow(26,len(columnTitle)-i-1))
            return corresponding_number

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(n)O(1)
JavaO(n)O(1)
JavaScriptO(n)O(1)
PythonO(n)O(1)
  • The logic is consistent across all four languages, with minor syntactic differences.
  • The JavaScript and Python implementations use Math.pow and pow respectively to calculate powers of 26, while C++ and Java use iterative multiplication.
  • All implementations are efficient and operate in linear time with constant space.
Scroll to Top