Skip to content

434. Number of Segments in a String

https://leetcode.com/problems/number-of-segments-in-a-string/

js
/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
  return s.split(/\s+/).filter(x => !!x).length
}
py
class Solution:

    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.split())