![]() |
VOOZH | about |
Given a column title as appears in an Excel sheet, return its corresponding column number.
column column number
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
Examples:
Input: A
Output: 1
Explanation: A is the first column so the output is 1.Input: AA
Output: 27
Explanation: The columns are in order A, B, ..., Y, Z, AA .. So, there are 26 columns after which AA comes.
The process is similar to binary to decimal conversion. Treat the Excel column title as a base-26 number, where A = 1, B = 2, ..., Z = 26. For example, to convert AB, the formula is 26 * 1 + 2.
To convert CDA,
3*26*26 + 4*26 + 1
= 26(3*26 + 4) + 1
= 26(0*26 + 3*26 + 4) + 1
Take the input as string and the traverse the input string from the left to right and calculate the result as follows:
result = 26*result + s[i] - 'A' + 1
2133