/58. Length of Last Word

58. Length of Last Word

Easy
Strings58.4% acceptance

Given a string input_text consisting of English letters and spaces, return the length of the last contiguous sequence of non-space characters (word) in input_text. A word is defined as a maximal substring of consecutive non-space characters.

Example 1

Input: openAI is great

Output: 5

Explanation: The last word is great with length 5.

Example 2

Input: data science rocks

Output: 5

Explanation: The last word is rocks with length 5.

Example 3

Input: python programming language

Output: 8

Explanation: The last word is language with length 8.

Constraints

  • 1 <= len(input_text) <= 10_000
  • input_text consists only of uppercase and lowercase English letters and spaces ( )
  • There is at least one word in input_text
Python (current runtime)

Case 1

Input: 'machine learning model'

Expected: 5

Case 2

Input: ' hello universe '

Expected: 8

Case 3

Input: 'abc def ghi jkl mno'

Expected: 3

Case 4

Input: 'singleword'

Expected: 10

Case 5

Input: ' spaced out '

Expected: 3