DEV Community

Jyothikrishna
Jyothikrishna

Posted on • Edited on

1 1

Leetcode question 58 Length of Last Word js solution

We can break the problem into 3 subproblems

  • Removing trailing spaces if they exist
  • Finding the words present in that processed string
  • And finally finding the length of the last word present in the processed string

We will tackle this subproblems one at a time.

Step 1 : Removing whitespaces at the beginning and end of the string

Since the string given to us might contain whitespaces in it, we need to remove them because if we don't remove them js treats them as different words.

To achieve this we make use of the trim method

const sWithOutWhiteSpaces = s.trim()
Enter fullscreen mode Exit fullscreen mode

Step 2 : Finding words present in string given

This step is very easy to do. We use split method achieve this.

const wordsInS = sWithOutWhiteSpaces.split(" ")
Enter fullscreen mode Exit fullscreen mode

Step 3 : Finding the last word in given string

Last word present in given string is the last element in array wordsInS. Length of that word is our required answer.

const lastWord = wordsInS[wordsInS.length - 1]
return lastWord.length
Enter fullscreen mode Exit fullscreen mode

Total solution

var lengthOfLastWord = function (s) {
  const sWithOutWhiteSpaces = s.trim();
  const wordsInS = sWithOutWhiteSpaces.split(" ");
  const lastWord = wordsInS[wordsInS.length - 1];
  return lastWord.length;
};

Enter fullscreen mode Exit fullscreen mode

If you submit at this point you will get these stats
stats before optimization

Optimization

We can optimize the time and space required to run this program by chaining all the methods and make it into a single statement thereby reducing the amount of space used by our program since we are no longer using memory to store intermediate values linke wordsInS.

Now our solution looks like this

var lengthOfLastWord = function (s) {
  return s.trim().split(" ").pop().length;
};
Enter fullscreen mode Exit fullscreen mode

Time and space complexity

  • Time: O(n)
  • Space: O(1)

After doing above mentioned optimizations you will get these stats

stats after optimization

I hope you found this article. If yes hit that like button.

Happy Hacking

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay