Simple O(n) time complexity and O(1) space complexity

amazon
Tags: #<Tag:0x00007f24355dbc80>

#1

Comment body goes here.
index for A-Z are as Follows:
A=1
B=2
:
V=22
:
Z=26

Lets assume string length is N

Ncolumm_Number=[N-1]column_Number*26+Index_of_Nth_character

Example:
string =“AVC”
index of A=1
index of AV : 126+22=48;
index of AVC : 48
26+3=1251;

code:
int Solution::titleToNumber(string A) {

if(A.size()==1)
{
    return A[0]-65+1;
}

int prelenght=A[0]-65+1;
for(int i=1;i<A.size();i++)
{
    prelenght=prelenght*26+A[i]-65+1;
}
return prelenght;

}