Skip to main content

Excel Sheet Column Number

Given a column title as appear in an Excel sheet, return its corresponding column number.

Example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Write your solution:
Test Case 1
Input: "A"
Output: 1
Test Case 2
Input: "Z"
Output: 26
Test Case 3
Input: "AA"
Output: 27
Test Case 4
Input: "AB"
Output: 28
Test Case 5
Input: "ZY"
Output: 701
Solution

Complexity:

  • Time complexity: O(n)
  • Space complexity: O(1)
function main(col) {
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const dict = Object.fromEntries(letters.split('').map((it, index) => [it, index + 1]));
const lettersLen = letters.length;
const colLen = col.length;

return col
.toUpperCase()
.split('')
.reduce((acc, letter, index) => acc + Math.pow(lettersLen, colLen - (index + 1)) * dict[letter], 0);
}