Excel Column Number

Easy
0/40
Average time to solve is 23m
profile
Contributed by
32 upvotes
Asked in companies
ZomatoExpedia GroupOracle

Problem statement

You have been given a 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 
...
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The only line of input contains a string S i.e. column title 
Constraints:
1 <= |S| <= 10
Input contains only uppercase English Alphabet letters

Time Limit : 1 sec
Output Format
The only line of output will print the column number corresponding to given column title 
Sample Input 1
AB
Sample Output 1
28
Sample Input 2
ZZZ
Sample Output 2
18278
Hint

Think of the question as converting a binary number into decimal form.

Approaches (1)
Base 26 Conversion

The process is very much similar to binary to decimal conversion. In this case, the base of the given system is to be taken as 26. Also, in this system 1 is represented as ‘A’, 2 is represented as ‘B’ and so on till 26 is represented as ‘Z’



For example- 

 

CDA can be converted as 3*26*26 + 4*26 + 1

                                          = 26(3*26 + 4) + 1

                                          = 26(0*26 + 3*26 + 4) + 1

 

AB can be converted as  1*26 + 2

Time Complexity

O(n), where n is the length of the input string. 

 

Since we are traversing each character of string only once.

Space Complexity

O(1)

 

As no extra space is required.

Code Solution
(100% EXP penalty)
Excel Column Number
Full screen
Console