Excel Sheet Column Number

easy math string

Problem

Given a string columnTitle (the column label in an Excel sheet), return its corresponding column number.

InputcolumnTitle = "ZY"
Output701
Bijective base 26: result starts at 0; for each char, result = result*26 + (c - 'A' + 1).

def title_to_number(s):
    n = 0
    for c in s:
        n = n * 26 + (ord(c) - ord('A') + 1)
    return n
function titleToNumber(s) {
  let n = 0;
  for (const c of s) {
    n = n * 26 + (c.charCodeAt(0) - 64);
  }
  return n;
}
class Solution {
    public int titleToNumber(String s) {
        int n = 0;
        for (int i = 0; i < s.length(); i++) {
            n = n * 26 + (s.charAt(i) - 'A' + 1);
        }
        return n;
    }
}
int titleToNumber(string s) {
    int n = 0;
    for (char c : s) {
        n = n * 26 + (c - 'A' + 1);
    }
    return n;
}
Time: O(|s|) Space: O(1)