1 분 소요

문제 링크

내 풀이 (60퍼센트 정답)

def solution(babbling):
    result = 0
    talks = ["aya", "ye", "woo", "ma"]
    for word in babbling:
        a = word
        for talk in talks:
            b = talk * 2
            if b not in word:
                if talk in a:
                    a = a.replace(talk, '') 
        if a == '':
            result += 1
    return result

다른 풀이


def solution(babbling):
    
    says = ["aya", "ye", "woo", "ma"]
    result = 0
    
    for i in range(len(babbling)):
    # babbling의 값을 변경해야 하므로 range(len(babbling)) 사용
    
        for say in says:
            if (say in babbling[i]) and (say*2 not in babbling[i]):
            # babbling[i] 안에 say가 있으면서도 say*2가 연속으로 있지 않을 시
            
                babbling[i] = babbling[i].replace(say, "*")
                # babbling[i]안의 say를 "*"로 바꾼다
                # 예) "ayamwoo" => "*m*"
        
        if all(char == "*" for char in babbling[i]):
            # babbling[i] 가 "*"로만 이루어져 있을시 result += 1
            result += 1
    
    return result

풀이 해석

babbling[i]에 말할수 있는 문자가 포함되고 연속되어 나오지 않으면 해당 문자를 *로 바꾸었다. 이를 통해 replace를 한 후에 생길 수 있는 오류에 대해 대비하였다. ex => {yayae} => aya를 ‘‘으로 replace하면 ye가 나와서 조건을 만족해버림

배울 점

  • replace를 ‘‘이 아닌 ‘*‘로 하는 사고
  • all 함수 사용법

댓글남기기