Length of Last Word
int Solution::lengthOfLastWord(const string A) {
int len = 0;
int i = A.size() - 1;
while(i >= 0 && A[i] == ' ')
i--;
while(i >= 0 && A[i] != ' ')
i--, len++;
return len;
}
Time Complexity: β
Space Complexity: β
Edge Case: What if there are spaces the end of the string "Hello Wolrd "
Last updated