잠 안 올 때 끄적끄적
[프로그래머스/파이썬/python3] Lv1. 문자열 나누기 본문
> 문제
> 코드
문제를 잘못 읽어서 한참 헤맸다.
"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)
아까우니까 기록은 해둬야지
'코딩 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/파이썬/python3] Lv1. [1차] 다트 게임 (1) | 2023.02.02 |
---|---|
[프로그래머스/파이썬] Lv1. 기사단원의 무기 (0) | 2023.01.29 |
[프로그래머스/파이썬/python] Lv1. 명예의 전당 (1) (0) | 2023.01.29 |
[프로그래머스/파이썬/python3] Lv1. 가장 가까운 글자 (0) | 2023.01.29 |
[프로그래머스/파이썬/python3] Lv1. 크기가 작은 부분 문자열 (0) | 2023.01.29 |