본문 바로가기
Leetcode

[LeetCode] Number of Segments in a String 파이썬 (white space as None)

by YGSEO 2021. 4. 16.
728x90
class Solution:
    def countSegments(self, s: str) -> int:
        
        return len([i for i in s.strip().split(' ') if i])

strip은 안전하게 양쪽 끝의 공백을 제거하는 용도로 넣어준 듯.

 

if i 라는 조건을 통해 whitespace가 아닐 경우만

새로운 list의 원소로 추가.


str.strip

str.strip([chars])

Return a copy of the string with the leading and trailing characters removed.

 

728x90

댓글