Coding Test Practice/Python

[프로그래머스 코딩테스트 연습 Lv. 1] 문자열 나누기

y2r1m 2023. 3. 13. 03:16

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

def solution(s):
    sep_cnt = 0
    x = s[0]
    xcnt = 0
    ocnt = 0
    for i, alp in enumerate(s):
        if alp == x:
            xcnt += 1
        else:
            ocnt += 1
            
        if xcnt == ocnt:
            if i == len(s) - 1: # 끝 글자이면
                break
            
            sep_cnt += 1
            x = s[i+1]
            xcnt = 0
            ocnt = 0
            continue
            
    answer = sep_cnt + 1
    return answer