We have discussed Conversion from column number to Excel Column name. In this post, reverse is discussed.
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
Input: A Output: 1 A is the first column so the output is 1. Input: AA Output: 27 The columns are in order A, B, ..., Y, Z, AA .. So, there are 26 columns after which AA comes.
Approach: The process is similar to binary to decimal conversion.
For example, to convert AB, the formula is 26 * 1 + 2.
As another example,
To convert CDA, 3*26*26 + 4*26 + 1 = 26(3*26 + 4) + 1 = 26(0*26 + 3*26 + 4) + 1
So it is very much similar to converting binary to decimal keeping the base as 26.
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
The result will be summation of
- Time Complexity: O(n), where n is length of input string.
- Space Complexity: O(1).
As no extra space is required.
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find Excel column name from a given column number
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Print an N x M matrix such that each row and column has all the vowels in it
- Maximum sum of a Matrix where each value is from a unique row and column
- Removing row or column wise duplicates from matrix of characters
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number, find the next smallest palindrome
- Function to find Number of customers who could not get a computer
- Find all strings formed from characters mapped to digits of a number
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Program to find remainder when large number is divided by 11
- Find the Number which contain the digit d
- Find total number of distinct years from a string
- Program to find remainder when large number is divided by r
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Find the number in a range having maximum product of the digits
- Find the minimum number of preprocess moves required to make two strings equal
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Find the number of players who roll the dice when the dice output sequence is given