Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

잠 안 올 때 끄적끄적

[프로그래머스/파이썬/python3] Lv1. 문자열 나누기 본문

코딩/프로그래머스

[프로그래머스/파이썬/python3] Lv1. 문자열 나누기

kerp 2023. 1. 29. 22:04

> 문제

> 코드

문제를 잘못 읽어서 한참 헤맸다. 

"s.count(x)"-"s.count(!x)" == 0 인 경우들을 분해하면 됐는데

문자의 개수를 각각 보고 같아질 때가 있는지를 확인한 것... 뚜쉬

저 입출력 예 #3에서 "aaabbacc〰️"가 아니라 "aaaabbacc〰️"정도만 됐어도 알아차렸을 텐데ㅠ 

그리고 코드도 길게 짜서 속상하다 ㅠㅠ 

일단 맞는 코드는

def solution(s):
    answer = 0
    cnt = 0
    i = 0
    while s:
        w = s[0]
        for i in range(len(s)):
            if s[i] == w:
                cnt += 1
            else:
                cnt -= 1
            if cnt == 0 or i == len(s) - 1:
                s = s[i+1:]
                answer += 1
                break
            
    return answer

내가 잘못 푼 코드는

def solution(s):
    answer = []
    di_cnt={}
    ans_str=""

    for ch in s:
        if ch not in di_cnt:
            newval = 1
        else:
            newval = di_cnt[ch]+1
        ans_str+=ch

        if newval in di_cnt.values():
            answer.append(ans_str)
            ans_str=""
            di_cnt={}

        else:
            di_cnt[ch]=newval

    if ans_str != "":
        answer.append(ans_str)
        
    return len(answer)

아까우니까 기록은 해둬야지