Simple JAVA solution with O(1) space complexity


#1

public class Solution {
public int titleToNumber(String A) {
int ans = 0;
int multiplier = 1;
for(int i = A.length() - 1 ; i >= 0 ; i–){
char ch = A.charAt(i);
int q = ch - ‘A’ + 1;
ans += q * (multiplier);
multiplier *= 26;
}

    return ans;
}

}


#2

Since you’re using a for loop that goes from size of the string to 0 the complexity is O(size of the string) and not O(1).


#3

sorry it was a typo in the title, I’ve corrected it now, thanks for spotting it out and letting me know :slight_smile: