Skip to content

Problem

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

Solve

A special base 26 to base 10 problem. That has character set ["A", "B", ..., "Z" ] with corresponds value [1,2,3,4,...,26] (no 0)

class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        sub_base26 = columnTitle[::-1]
        true_value = 0
        for index, char in enumerate(sub_base26):
            true_value += (ord(char) - ord("A") + 1) * (26 ** index)
        return true_value

The fun begin at 168. Excel Sheet Column Title, where you want to revert this process. This is other hand need more thought on how these type of number work


Last update : August 23, 2023
Created : August 16, 2023