알고리즘 24 - Length of Last Word

박진현·2021년 7월 15일
0

Q.

Given a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0.

A word is a maximal substring consisting of non-space characters only.

Example 1:

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

Input: s = " "
Output: 0

Constraints:

1 <= s.length <= 104
s consists of only English letters and spaces ' '.

A)

var lengthOfLastWord = function(s) {
  
  const strToArr = s.trim().split(' ')
  
  return strToArr[strToArr.length-1].length
  
};

Runtime: 68 ms, faster than 92.32% of JavaScript online submissions for Length of Last Word.
Memory Usage: 39 MB, less than 18.46% of JavaScript online submissions for Length of Last Word

profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글