All posts
1 min read

[018] Length Of Last Word

On this page

Problem

Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s = “Hello World”, return 5.

Read

Not hard — you just have to handle the edge cases.

Solution

C++ version, approach 1:

   // walk from right to left
  class Solution {
    public:
        int lengthOfLastWord(string s) {
            int length = 0;
            for (int index = s.length() - 1; index >= 0; index--){
                char c = s.at(index);
                if (c != ' '){
                    length++;
                }
                else if (c == ' ' && length != 0){
                    return length;
                }
            }
            return length;
        }
    }

The LeetCode OJ result is below:

leetCode C++1

;

C++ version, approach 2:

    // use STL — let the standard library do the work
class Solution {
    public:
        int lengthOfLastWord(string s) {
            auto left = find_if(s.rbegin(), s.rend(), ::isalpha);
            auto right = find_if_not(left, s.rend(), ::isalpha);
            return distance(left, right);
        }
    }
    

The LeetCode OJ result is below:

leetCode C++2

;