파이썬 알고리즘 : 옹알이 (2)

set

2024년 4월 6일 알고리즘 문제풀이

문제

난이도

Lv. 1

코드

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
def search_index(arr,k):
    result = -1
    if k in arr:
        result = arr.index(k)
    return result

def solution(babbling):
    answer = 0
    arr = ["aya", "ye", "woo", "ma"]
    for word in babbling:
        idx = 0
        n = len(word)
        last_find_idx = -1
        while idx <= n:
            if not n:
                answer += 1
                break
            tmp = word[:idx+1]
            search_result = search_index(arr,tmp)
            if search_result != -1 and search_result != last_find_idx:
                last_find_idx = search_index(arr,tmp)
                word = word[idx+1:]
                n = len(word)
                idx = 0
            else:
                idx += 1
                
    return answer