Press "Enter" to skip to content

Posts tagged as “segment”

花花酱 LeetCode 434. Number of Segments in a String

Problem

题目大意:返回字符串中的非空白字符连续的字串数量。

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

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

Solution

Be aware of special cases: like ”      “.

Time complexity: O(n)

Space complexity: O(1)

C++

Python3