L1 : 문자열 나누기 Python

jhyunn·2023년 1월 12일
0

Programmers

목록 보기
11/69

L1 : 문자열 나누기 Python

https://school.programmers.co.kr/learn/courses/30/lessons/140108

def solution(s):
    cnt = 0
    x_cnt, nonx_cnt = 0, 0
    
    for i, c in enumerate(s):
        if x_cnt == 0:
            x = c
        
        if x == c:
            x_cnt += 1
        else:
            nonx_cnt += 1

        if x_cnt == nonx_cnt:
            cnt += 1
            x_cnt, nonx_cnt = 0, 0
            
    if x_cnt+nonx_cnt != 0:
        return cnt+1
    
    return cnt

x, x가 아닌 것의 수를 비교하여 같아질 때마다, 카운트를 초기화

profile
https://github.com/Sungjeonghyun

0개의 댓글