C# Solution


  • 0
        public int CountSegments(string s) {
            if (s == null || s == string.Empty)
                return 0;
                
            int count = 0;
            string trimmedString = s.Trim();
            
            if(trimmedString == string.Empty)
                return 0;
            
            for(int index = 1; index <= trimmedString.Length - 1; index++)
                if ((int)trimmedString[index] == 32 && (int)trimmedString[index - 1] != 32)
                    count++;
            
            return ++count;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.