3 line Java Solution without pow function


#1

public class Solution {
public int titleToNumber(String A) {

    int ans=0;
    for(char ch:A.toCharArray()) ans=(ans*26)+(int)(ch-64);
    return ans;
}

}


#2

can you please explain it a little bit more why you did (ch-64); and what logic are you applying?


#3

this works too
64 is the ascii value for ‘A’
public class Solution {
public int titleToNumber(String str) {
int ans=0;
for(int i=0;i<str.length();i++){
ans= (ans*26)+str.charAt(i)-‘A’+1;
}
return ans;

    }
}