C# solution


  • 0
    public class Solution {
        public int CountSegments(string s) {
              s = s.Trim();
                
                if (s.Length == 0)
                {
                    return 0;
                }
    
    
                string[] split = s.Split(' ');
                List<string> list = new List<string>();
                for (int i = 0; i < split.Length; i++)
                {
                    if (split[i].Length > 0)
                    {
                        list.Add(split[i]);
                    }
                }
                return list.Count;
    
            
        }
    }
    

Log in to reply
 

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