HomeLeetcode171. Excel Sheet Column Number - Leetcode Solutions

171. Excel Sheet Column Number – Leetcode Solutions

Description

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 
...

Examples:

Example 1:

Input: columnTitle = "A"
Output: 1

Example 2:

Input: columnTitle = "AB"
Output: 28

Example 3:

Input: columnTitle = "ZY"
Output: 701

Solution in Python

Python
class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        # Initialize the result to 0
        result = 0
        
        # Iterate over each character in the string
        for char in columnTitle:
            # Convert the character to its corresponding value (A -> 1, B -> 2, ..., Z -> 26)
            # ord('A') gives 65, so ord(char) - ord('A') + 1 gives the correct value
            char_value = ord(char) - ord('A') + 1
            
            # Update the result by shifting previous result by 26 (like base-26) and adding current character's value
            result = result * 26 + char_value
        
        # Return the final result which is the column number
        return result

Explanation:

  1. Initial Setup:
    • result is initialized to 0. This variable will hold the cumulative value as we process each character in the columnTitle string.
  2. Iteration through Characters:
    • The code loops through each character in the columnTitle. For each character, it calculates its corresponding value:
      • ord(char) - ord('A') + 1 is used to convert the character to its respective column number. For example, ‘A’ becomes 1, ‘B’ becomes 2, and so on up to ‘Z’, which becomes 26.
  3. Updating the Result:
    • The result is updated by treating it as a number in a base-26 numeral system. For each character, the current result is multiplied by 26 (to shift it one place to the left in base-26) and then the value of the current character is added to it. This process effectively accumulates the final column number.
  4. Return the Result:
    • After processing all the characters, the final value in result is returned, which represents the column number corresponding to the input title.

This code handles all cases within the constraints, including single-letter titles and titles with multiple characters.

Solution in Javascript

JavaScript
/**
 * @param {string} columnTitle
 * @return {number}
 */
var titleToNumber = function(columnTitle) {
    // Initialize the result variable to 0
    let result = 0;
    
    // Iterate over each character in the columnTitle string
    for (let i = 0; i < columnTitle.length; i++) {
        // Get the current character
        let char = columnTitle[i];
        
        // Convert the character to its corresponding value (A -> 1, B -> 2, ..., Z -> 26)
        // charCodeAt(0) gets the ASCII value of the character, and subtracting 64 adjusts it to A=1, B=2, ..., Z=26
        let charValue = char.charCodeAt(0) - 'A'.charCodeAt(0) + 1;
        
        // Update the result by shifting previous result by 26 (like base-26) and adding current character's value
        result = result * 26 + charValue;
    }
    
    // Return the final result which is the column number
    return result;
};

Solution in Java

Java
class Solution {
    public int titleToNumber(String columnTitle) {
        // Initialize the result variable to 0
        int result = 0;
        
        // Iterate over each character in the columnTitle string
        for (int i = 0; i < columnTitle.length(); i++) {
            // Get the current character
            char currentChar = columnTitle.charAt(i);
            
            // Convert the character to its corresponding value (A -> 1, B -> 2, ..., Z -> 26)
            // 'A' is 65 in ASCII, so currentChar - 'A' + 1 gives the correct value
            int charValue = currentChar - 'A' + 1;
            
            // Update the result by shifting previous result by 26 (like base-26) and adding current character's value
            result = result * 26 + charValue;
        }
        
        // Return the final result which is the column number
        return result;
    }
}

Solution in C#

C#
public class Solution {
    public int TitleToNumber(string columnTitle) {
        // Initialize the result variable to 0
        int result = 0;

        // Iterate over each character in the columnTitle string
        for (int i = 0; i < columnTitle.Length; i++) {
            // Get the current character
            char currentChar = columnTitle[i];
            
            // Convert the character to its corresponding value (A -> 1, B -> 2, ..., Z -> 26)
            // 'A' has an ASCII value of 65, so currentChar - 'A' + 1 gives the correct value
            int charValue = currentChar - 'A' + 1;
            
            // Update the result by shifting the previous result by 26 (like base-26) and adding the current character's value
            result = result * 26 + charValue;
        }

        // Return the final result which is the column number
        return result;
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular