Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

171. Excel Sheet Column Number 🌟

Given a string columnTitle that represents the 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
...

Intuitive Solution (AC)

Code

class Solution {
public:
    int titleToNumber(string columnTitle)
    {
        int ans = 0;
        for (auto x : columnTitle) {
            int temp = x - 'A' + 1; // 'A'-'A' will be 0 so add 1 in it.
            ans = ans * 26 + temp;
        }
        return ans;
    }
};