Press "Enter" to skip to content

LeetCode of the day – 58 Length of Last Word (Easy)

Last updated on July 13, 2024

Approach – Regex

This one was pretty straight forward though I fell right within the average performance. Time Complexity O(N).

Here is my solution, it could be optimized though this was my accepted solution with good enough performance:

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function (s) {
  let word = s
    .replace(/^\s+|\s+$/g, "")
    .split(" ")
    .pop();

  return word.length;
};
Leave a Reply

Your email address will not be published. Required fields are marked *