58. Length of Last Word

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.

Example:

Input: "Hello World"
Output: 5

Solution:

JavaScript specific solutions:

ONE

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
  return (/\w+$/.exec(s) || [''])[0].length
};

TWO

Super fast. split will guarantee that there is at least one item in the resulted array.

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
  return s.trim().split(' ').pop().length
};

THREE

General solution.

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
  let end = s.length - 1
  while (end >= 0 && s[end] === ' ') {
    end--
  }

  let start = end
  while (start >= 0 && s[start] !== ' ') {
    start--
  }

  return end - start
};