Skip to content

Latest commit

 

History

History
40 lines (33 loc) · 826 Bytes

171_excelSheetColumnNumber.md

File metadata and controls

40 lines (33 loc) · 826 Bytes

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)

  • We can observe that the number formed is like 26 base number system represented in capital letters.
  • We can for number with char*26+carry formula.
  • TC: O(N)
  • SC: O(1)

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;
    }
};