Class Solution

java.lang.Object
g0001_0100.s0058_length_of_last_word.Solution

public class Solution extends Object
58 - Length of Last Word.<p>Easy</p> <p>Given a string <code>s</code> consisting of some words separated by some number of spaces, return <em>the length of the <strong>last</strong> word in the string.</em></p> <p>A <strong>word</strong> is a maximal substring consisting of non-space characters only.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;Hello World&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> The last word is &ldquo;World&rdquo; with length 5.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &quot; fly me to the moon &quot;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The last word is &ldquo;moon&rdquo; with length 4.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;luffy is still joyboy&rdquo;</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The last word is &ldquo;joyboy&rdquo; with length 6.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>4</sup></code></li> <li><code>s</code> consists of only English letters and spaces <code>' '</code>.</li> <li>There will be at least one word in <code>s</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • lengthOfLastWord

      public int lengthOfLastWord(String s)