Skip to content

[434] Number of Segments in a String

https://leetcode.com/problems/number-of-segments-in-a-string/description/

  • algorithms
  • Easy (36.38%)
  • Source Code: 434.number-of-segments-in-a-string.py
  • Total Accepted: 53.7K
  • Total Submissions: 145.9K
  • Testcase Example: '"Hello, my name is John"'

Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.

Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John" Output: 5

python
class Solution(object):
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.split())

Last updated: