LeetCode – 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 Solution { public int countSegments(String s) { if(s.length() == 0) return 0; int result =0; if(s.charAt(0)!=' '){ result++; } for(int i=0;i 

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"LeetCode – 434. Number of Segments in a String"

122
0 0 122

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top