본문 바로가기
코딩 테스트 연습

[알고리즘] 프로그래머스 - 옹알이(1)

by 코드뭉치 2023. 5. 2.

옹알이

 

babbling내에 해당 문자열이 있을 시, replace로 1로 바꿈(1개만)

다 바꾼 뒤 문자열이 isdigit(), 즉 숫자로만 이루어져 있다면 cnt +=1 

def solution(babbling):
    ong_al = ["aya", "ye", "woo", "ma"]
    cnt = 0
    for i in babbling:
        for j in ong_al:
            if j in i:
                i = i.replace(j, "1", 1)
        if i.isdigit():
            cnt += 1
    return cnt

 

정규 표현식을 사용한 방법

import re

def solution(babbling):
    regex = re.compile('^(aya|ye|woo|ma)+$')
    cnt=0
    for e in babbling:
        if regex.match(e):
            cnt+=1
    return cnt

 

댓글