171. Excel Sheet Column Number

Description

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

Constraints

Approach

  • GeeksforGeeks

  • ProgramCreek

  • YouTube

Examples

Input: "A"

Output: 1

Solutions

/**
 * Time complexity : O(N) where N is the number of characters in the input string.
 * Space complexity : O(1)
 */

class Solution {
    public int titleToNumber(String s) {
        int result = 0;
        
        for(int i = 0; i < s.length(); i++) {
            result *= 26;
            result += (s.charAt(i)-'A')+1;
        }
        
        return result;
    }
}

Follow up

Last updated

Was this helpful?