{x}
blog image

Excel Sheet Column Number

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 <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

Solution Explanation: Excel Sheet Column Number

The problem asks to convert an Excel column title (e.g., "A", "AB", "ZY") into its corresponding column number (e.g., 1, 28, 701). The key observation is that Excel column titles represent a base-26 number system, where 'A' = 1, 'B' = 2, ..., 'Z' = 26.

Approach:

The solution uses a straightforward iterative approach. We traverse the input string columnTitle from left to right. For each character:

  1. We convert it to its numerical equivalent (A=1, B=2,..., Z=26). This is done by subtracting the ASCII value of 'A' and adding 1.
  2. We multiply the current result ans by 26 to shift the existing digits to the next higher place value (similar to multiplying by 10 in base-10).
  3. We add the numerical value of the current character to ans.

This process effectively converts the base-26 representation to a base-10 integer.

Time Complexity: O(n), where n is the length of the input string. We iterate through the string once.

Space Complexity: O(1). We only use a constant amount of extra space to store variables.

Code Implementation (Python):

class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        ans = 0
        for c in columnTitle:
            ans = ans * 26 + (ord(c) - ord('A') + 1)  # Convert char to int, base 26 calculation
        return ans

Code Implementation (Java):

class Solution {
    public int titleToNumber(String columnTitle) {
        int ans = 0;
        for (char c : columnTitle.toCharArray()) {
            ans = ans * 26 + (c - 'A' + 1); //Convert char to int, base 26 calculation
        }
        return ans;
    }
}

Code Implementation (C++):

class Solution {
public:
    int titleToNumber(string columnTitle) {
        int ans = 0;
        for (char c : columnTitle) {
            ans = ans * 26 + (c - 'A' + 1); //Convert char to int, base 26 calculation
        }
        return ans;
    }
};

Code Implementation (Go):

func titleToNumber(columnTitle string) int {
	ans := 0
	for _, c := range columnTitle {
		ans = ans*26 + int(c-'A'+1) //Convert rune to int, base 26 calculation
	}
	return ans
}
 

Code Implementation (TypeScript):

function titleToNumber(columnTitle: string): number {
    let ans: number = 0;
    for (let i = 0; i < columnTitle.length; i++) {
        ans = ans * 26 + (columnTitle.charCodeAt(i) - 'A'.charCodeAt(0) + 1); //Convert charCode to int, base 26 calculation
    }
    return ans;
}

Code Implementation (C#):

public class Solution {
    public int TitleToNumber(string columnTitle) {
        int ans = 0;
        foreach (char c in columnTitle) {
            ans = ans * 26 + (c - 'A' + 1); //Convert char to int, base 26 calculation
        }
        return ans;
    }
}

All the implementations follow the same logic, differing only in syntax and data type handling specific to each language. The core algorithm remains consistent for efficient base-26 to base-10 conversion.