LeetCode 58 is another easy problem.1 For this problem, we get a string and we have to find the length of the last word. Here are the examples:2

Example 1:
  Input: s = "Hello World"
  Output: 5

Example 2:
  Input: s = "   fly me   to   the moon  "
  Output: 4

There are two main solutions that I can think of. The first solution is to loop forward through the array and track the length of each word and drop it once arriving at a space char (' '). That, though, will be slower than iterating backwards through the input string.

func lengthOfLastWord(s string) int {
    i := len(s) - 1
    length := 0

    // ignore preceding spaces
    for ; i >= 0 && s[i] == ' '; i-- { }

    // count length of last word
    for ; i >= 0 && s[i] != ' '; i-- {
        length++
    }

    return length
}

This solution has a runtime complexity of \(O(n)\), but in practice it is less complicated than the forward version and should run for less time. Less importantly, it beats 100% of submissions on LeetCode, so we’re done.

Till next time, friends.


  1. I know. I am supposed to be doing dynamic programming problems. I’m going to get back to them. It is a very busy week for me. ↩︎

  2. I removed the third example that they provide because I see it as a spoiler for One Piece and I don’t approve of that. ↩︎