Post

leetcode(리트코드)-434 Number of Segments in a StringNumber of Segments in a String(PYTHON)

leetcode 434 - Number of Segments in a StringNumber of Segments in a String 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • 문자열이 들어옵니다. 어절의 개수를 리턴하세요.

5. code

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def countSegments(self, s: str) -> int:
        if len(s) == 0  or s.count(' ') == len(s) :
             return 0
        res = 0 
        idx = 0 
        while idx < len(s) : 
            if s[idx] != ' ': 
                res += 1
                while idx < len(s) and s[idx] != ' ':
                    idx+=1
            else: 
                idx += 1
        return res
        

6. 결과 및 후기, 개선점

필요시 c++로 짜드립니다.

설명이 필요하다면 댓글을 달아주세요.

This post is licensed under CC BY 4.0 by the author.