forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_434.java
32 lines (29 loc) · 815 Bytes
/
_434.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fishercoder.solutions;
/**
* 434. Number of Segments in a String
*
* 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*/
public class _434 {
public static class Solution1 {
public int countSegments(String s) {
if (s == null || s.isEmpty()) {
return 0;
}
String[] segments = s.split(" ");
int count = 0;
for (String seg : segments) {
if (seg.equals("")) {
continue;
}
count++;
}
return count;
}
}
}