Leetcode 171. Excel Sheet Column Number

Problem Explanation

In this problem, we are given a string representing a column title from an Excel spreadsheet. In Excel, the columns are labeled letters A to Z. After Z, the labels continue with AA, AB, AC and so on. Our goal is to convert this column label into the corresponding column number. For example, the label "A" corresponds to column number 1, "B" is 2, "Z" is 26, "AA" is 27, "AB" is 28, and so on.

This is similar to converting a number from base 26 to a base 10 number system. We loop through the letters of the string from left to right. For each letter, we multiply the current number by 26 (analogous to "shifting" in decimal numbers where you multiply by 10) and add the value of the current letter (1 for 'A', 2 for 'B', and so on up to 26 for 'Z'). This gives us the final column number.

Let's walk through an example:

Suppose the input string is "AB".

  • For the first letter 'A', the corresponding number is 1.
  • The next letter is 'B'. To incorporate this, we multiply the current number (1) by 26 and add the value of 'B', which is 2. Now our number is 1*26 + 2 = 28.
  • Therefore, the column label "AB" corresponds to the column number 28.

Approaches

The approach consists of the following steps:

  • Initialize a result variable as 0.
  • Loop through the string from left to right.
  • For each character, multiply the result by 26 and add the numerical value of the character which is given by the ASCII value of the letter minus the ASCII value of 'A' plus 1.
  • In the end, the result has the final column number.

The time complexity of this approach is O(n), where n is the length of the input string. We are simply iterating through the string once.

Python Solution

1
2python
3class Solution:
4    def titleToNumber(self, s: str) -> int:
5        # Initialize result as 0
6        result = 0
7        # Loop through the string
8        for char in s:
9            # Get the numerical value of the character
10            numVal = ord(char) - ord('A') + 1
11            # Multiply the result by 26 and add the numerical value
12            result = result * 26 + numVal
13        # Return the result
14        return result

Java Solution

1
2java
3class Solution {
4    public int titleToNumber(String s) {
5        // Initialize result as 0
6        int result = 0;
7        // Loop through the string
8        for (char c : s.toCharArray()) {
9            // Get the numerical value of the character
10            int numVal = c - 'A' + 1;
11            // Multiply the result by 26 and add the numerical value
12            result = result * 26 + numVal;
13        }
14        // Return the result
15        return result;
16    }
17}

JavaScript Solution

1
2javascript
3var titleToNumber = function(s) {
4    // Initialize result as 0
5    var result = 0;
6    // Loop through the string
7    for (var i = 0; i < s.length; i++) {
8        // Get the numerical value of the character
9        var numVal = s.charCodeAt(i) - 'A'.charCodeAt(0) + 1;
10        // Multiply the result by 26 and add the numerical value
11        result = result * 26 + numVal;
12    }
13    // Return the result
14    return result;
15};

C++ Solution

1
2c++
3class Solution {
4public:
5    int titleToNumber(string s) {
6        // Initialize result as 0
7        int result = 0;
8        // Loop through the string
9        for (char c : s) {
10            // Get the numerical value of the character
11            int numVal = c - 'A' + 1;
12            // Multiply the result by 26 and add the numerical value
13            result = result * 26 + numVal;
14        }
15        // Return the result
16        return result;
17    }
18};

C# Solution

1
2csharp
3public class Solution {
4    public int TitleToNumber(string s) {
5        // Initialize result as 0
6        int result = 0;
7        // Loop through the string
8        foreach (char c in s) {
9            // Get the numerical value of the character
10            int numVal = c - 'A' + 1;
11            // Multiply the result by 26 and add the numerical value
12            result = result * 26 + numVal;
13        }
14        // Return the result
15        return result;
16    }
17}

Go Solution

1
2go
3func titleToNumber(s string) int {
4    // Initialize result as 0
5    var result int
6    // Loop through the string
7    for _, c := range s {
8        numVal := int(c - 'A' + 1)
9        result = result*26 + numVal
10    }
11    // Return the result
12    return result
13}

R Solution

1
2R
3titleToNumber <- function(s) {
4  # Initialize result as 0
5  result <- 0
6  # Loop through the string
7  for(i in 1:nchar(s)){
8    numVal <- as.integer(charToRaw(substr(s, i, i))) - as.integer(charToRaw("A")) + 1
9    result <- result * 26 + numVal
10  }
11  # Return the result
12  return(result)
13}

Clojure Solution

1
2clojure
3(defn titleToNumber [s]
4  (let [conv (fn [char] (- (int char) 64))]
5    (reduce #(+ (* %1 26) (conv %2)) 0 s)))

Ruby Solution

1
2ruby
3def title_to_number(s)
4    # Initialize result as 0
5    result = 0
6    # Loop through the string
7    s.each_byte do |c|
8        # Get the numerical value of the character
9        num_val = c - 64
10        # Multiply the result by 26 and add the numerical value
11        result = result * 26 + num_val
12    end
13    # Return the result
14    return result
15end

In conclusion, converting an Excel column title into its corresponding number can be solved using a simple algorithm along with the ASCII values of the English alphabet. This problem can be solved in various programming languages including Python, Java, and JavaScript with a time complexity of O(n), where n is the length of the input string. The provided solutions in different programming languages utilize a similar approach, emphasizing the algorithmic aspect of the problem rather than focusing on a particular language syntax.


Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ