Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 537 Bytes

434_number_of_segments_in_a_string.md

File metadata and controls

23 lines (20 loc) · 537 Bytes

Given a string s, return the number of segments in the string.

A segment is defined to be a contiguous sequence of non-space characters.

Example 1:

Input: s = "Hello, my name is John"
Output: 5
Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]

Example 2:

Input: s = "Hello"
Output: 1

Solution

class Solution:
    def countSegments(self, s: str) -> int:
        return len(s.split())