171_Excel Sheet Column Number

Given a column title as appear 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: "A"

Output: 1

Example 2:

Input: "AB"

Output: 28

Example 3:

Input: "ZY"

Output: 701

Solution:

Idea:

  • This problem is basically the problem of converting a string representing a base 26 number to the corresponding integer.

  • Note that "A" correspond to 1 not 0.

Time Complexity: O(n)O(n) where n is the number of characters in the input string.

Space Complexity: O(1)O(1)

Code 1: Iterative approach

Code 2: Recursive approach

Last updated