public class Solution { public int CountSegments(string s) { s = s.Trim(); if (s.Length == 0) { return 0; } else { var whitespacecount = 0; var prewhiteindex = -1; for (int i = 0; i < s.Length; i++) { var c = s[i]; if (char.IsWhiteSpace(c)) { if (prewhiteindex + 1 != i) { whitespacecount++; } prewhiteindex = i; } } return whitespacecount + 1; } } }
https://leetcode.com/problems/number-of-segments-in-a-string/#/description