2022-03-27 20:52:13 +08:00
< 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 > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:52:13 +08:00
< pre >
< strong > Input:< / strong > s = " Hello, my name is John"
< strong > Output:< / strong > 5
< strong > Explanation:< / strong > The five segments are [" Hello," , " my" , " name" , " is" , " John" ]
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:52:13 +08:00
< pre >
< strong > Input:< / strong > s = " Hello"
< strong > Output:< / strong > 1
< / pre >
< p > < / 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 > " !@#$%^& *()_+-=' ,.:" < / code > .< / li >
< li > The only space character in < code > s< / code > is < code > ' ' < / code > .< / li >
< / ul >