Simple Java Solution o(n)


  • 0
    V
    
    class Solution {
        public int countSegments(String s) 
        {
        		if (s.length() == 0) return 0;
        		int count  = 0;
        		String [] str = s.split(" ");
        		
        		for (int i = 0; i<str.length;i++)
        		{
        			if (!str[i].matches(""))
        			{
        				count++;
        			}
        		}
        		return count;
                    
        }
    }
    
    

Log in to reply
 

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