Easiest solution in c++ O(n) complexity


#1

int Solution::lengthOfLastWord(const string A) {
int n=A.length();
int i=n-1;
while(A[i]==’ ')
i–;
int j=0;
while((A[i]>=‘a’ && A[i]<=‘z’) || (A[i]>=‘A’ && A[i]<=‘Z’))
{
i–;
j++;
}
return j;
}


#2

Your solution is O(n)


#3

Thanks it was written by mistake