class Solution(object):
# https://leetcode.com/problems/number-of-segments-in-a-string/solution/
# def countSegments(self, s):
# """
# :type s: str
# :rtype: int
# """
# return len(s.split())
def countSegments(self, s):
segment_count = 0
for i in range(len(s)):
if (i == 0 or s[i-1] == ' ') and s[i] != ' ':
segment_count += 1
return segment_count
Download Number of Segments in a String.pyLeetcode 434 Number of Segments in a String problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.