- Our standard number system has digits 0 to 9, which is a base 10 system.
- In this problem we have letters A to Z, so we would like to say this is a base 26 system. However, we have no way to represent 0, so the analogy to a "base something" system cannot be made.
class Solution {
public int titleToNumber(String s) {
if (s == null) {
return -1;
}
int num = 0;
for (int i = 0; i < s.length(); i++) {
num *= 26; // 26 possible letters
num += s.charAt(i) - 'A' + 1;
}
return num;
}
}
- Time Complexity: O(n)
- Space Complexity: O(1)