58. Length of Last Word - Easy

Given a string s consisting of words and spaces, return the length of the last word in the string. A word is. a maximal substring consisting of non-space characters only.

Example:
Input: s = "Hello World"

Output: 5

Explanation: The last word is "World" with length 5.

Constraints:

  • 1 <= s.length <= 10**4
  • s consists of only English letters and spaces ' '.
  • There will be at least one word in s.

Solution:
var lenghtOfLastWord = function(s) {

     return s.trim().split(' ').pop().length;

}

Thoughts:
Super simple. 10/10 for confidence and understanding.

Comments

Popular posts from this blog

28. Find the Index of the First Occurence in a String - Easy

121. Best Time to Buy and Sell Stock - Easy