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

 

프로그래머스

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

programmers.co.kr

 

구현할게 많아 귀찮은(?) 문제입니다.

index를 밀어가면서 주어진 4개의 문자열이 나오는지를 확인해주면 되고, 연속해서 쓰이지 않도록 이전 문자열을 기억해줘야 합니다. 조건을 만족하지 못하거나 문자열을 전부 탐색해서 성공했을때 반복문을 끝내는 부분만 신경쓰면 어렵지 않게 풀립니다.

 

def solution(babbling):
    answer = 0

    for b in babbling:
        idx = 0
        flag = -1
        while idx < len(b):
            if b[idx] == 'a':
                if flag != 0 and idx + 2 < len(b) and b[idx+1] == 'y' and b[idx + 2] == 'a':
                    flag = 0
                    idx += 3
                else:
                    flag = -1
                    break
            elif b[idx] == 'y':
                if flag != 1 and idx + 1 < len(b) and b[idx+1] == 'e':
                    flag = 1
                    idx += 2
                else:
                    flag = -1
                    break
            elif b[idx] == 'w':
                if flag != 2 and idx + 2 < len(b) and b[idx+1] == 'o' and b[idx + 2] == 'o':
                    flag = 2
                    idx += 3
                else:
                    flag = -1
                    break
            elif b[idx] == 'm':
                if flag != 3 and idx + 1 < len(b) and b[idx+1] == 'a':
                    flag = 3
                    idx += 2
                else:
                    flag = -1
                    break
            else:
                flag = -1
                break

        if flag != -1:
            answer += 1

    return answer

print(solution(	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]))

 

728x90

+ Recent posts