<jupyter_start><jupyter_text>说明：    给您一个字符串s，返回字符串中的段数。    段定义为非空格字符的连续序列。Example 1:    Input: s = "Hello, my name is John"    Output: 5    Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]Example 2:    Input: s = "Hello"    Output: 1Example 3:    Input: s = "love live! mu'sic forever"    Output: 4Example 4:    Input: s = ""    Output: 0Constraints:    1、0 <= s.length <= 300    2、s consists of lower-case and upper-case English letters, digits or one of the following characters "!@$%^&*()_+-=',.:".    3、The only space character in s is ' '.<jupyter_code>class Solution:
    def countSegments(self, s: str) -> int:
        if not s: return 0
        stack = []
        res = 0
        for a in s:
            if a != ' ':
                stack.append(a)
            elif stack:
                res += 1
                stack = []
                
        if stack:
            res += 1
        return res
solution = Solution()
solution.countSegments("Hello, my name is John")
print(ord('a'), ord('z'))
print(ord('A'), ord('Z'))
"a, a, a, a, a, eaefa"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14620938628158844, 'fraction_numerical': 0.017148014440433214, 'mean_word_length': 2.2238372093023258, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '596697', 'license': 'Apache-2.0', 'n_tokens_mistral': 401, 'n_tokens_neox': 388, 'n_words': 118, 'path': 'String/1001/434. Number of Segments in a String.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '1108'}