Skip to content

Commit e01bed1

Browse files
authored
Create 434_Number_of_Segments_in_a_String.md
1 parent 25f1ffc commit e01bed1

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 434. Number of Segments in a String
2+
3+
You are given a string s, return the number of segments in the string.
4+
5+
A segment is defined to be a contiguous sequence of non-space characters.
6+
7+
8+
9+
Example 1:
10+
11+
Input: s = "Hello, my name is John"
12+
Output: 5
13+
Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]
14+
Example 2:
15+
16+
Input: s = "Hello"
17+
Output: 1
18+
Example 3:
19+
20+
Input: s = "love live! mu'sic forever"
21+
Output: 4
22+
Example 4:
23+
24+
Input: s = ""
25+
Output: 0
26+
27+
28+
29+
30+
31+
```python
32+
def countSegments(self, s: str) -> int:
33+
if s is '': return 0
34+
return len(s.split())
35+
```
36+
37+
38+
```
39+
Runtime: 28 ms, faster than 74.58% of Python3 online submissions for Number of Segments in a String.
40+
Memory Usage: 14.2 MB, less than 100.00% of Python3 online submissions for Number of Segments in a String.
41+
```

0 commit comments

Comments
 (0)