A Java Solution using split()


  • 0

    Here is a Java Solution used Split

    
    
        public int countSegments(String s) {
            if(s == null || s.equals("") )
                return 0;
            List<String> list = new ArrayList<>();
            String[] segs = s.split(" ");
            for(String ss:segs){
                if(!ss.equals("")){
                    list.add(ss);
                    
                }
            }
            return list.size();
        }
    

Log in to reply
 

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