Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
Constraints
Approach
Links
GeeksforGeeks
ProgramCreek
YouTube
Examples
Input: "A"
Output: 1
Input: "AB"
Output: 28
Input: "ZY"
Output: 701
Input: "FXSHRXW"
Output: 2147483647
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;
}
}