java.lang.Object
g0401_0500.s0434_number_of_segments_in_a_string.Solution

public class Solution extends Object
434 - Number of Segments in a String.<p>Easy</p> <p>Given a string <code>s</code>, return <em>the number of segments in the string</em>.</p> <p>A <strong>segment</strong> is defined to be a contiguous sequence of <strong>non-space characters</strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;Hello, my name is John&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> The five segments are [&ldquo;Hello,&rdquo;, &ldquo;my&rdquo;, &ldquo;name&rdquo;, &ldquo;is&rdquo;, &ldquo;John&rdquo;]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;Hello&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= s.length <= 300</code></li> <li><code>s</code> consists of lowercase and uppercase English letters, digits, or one of the following characters <code>&quot;!@#$%^&*()_+-=',.:&quot;</code>.</li> <li>The only space character in <code>s</code> is <code>' '</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • countSegments

      public int countSegments(String s)