0
点赞
收藏
分享

微信扫一扫

434. Number of Segments in a String

嚯霍嚯 2022-08-03 阅读 53


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

class Solution {
public int countSegments(String s) {
int segmentCount = 0;

for (int i = 0; i < s.length(); i++) {
if ((i == 0 || s.charAt(i-1) == ' ') && s.charAt(i) != ' ') {
segmentCount++;
}
}

return


举报

相关推荐

0 条评论