Programmers - 모음 사전

SJ0000·2022년 5월 10일

문제 링크

사전 순서상 다음에 올 단어를 구하는 next() 를 만들어서 순회

보기 불편해서 AEIOU를 01234로 바꾸었다

def solution(word):
    maxlen = 5
    maxIndex = 4

    def wordToIntArr(word):
        word = word.replace('A', '0')
        word = word.replace('E', '1')
        word = word.replace('I', '2')
        word = word.replace('O', '3')
        word = word.replace('U', '4')
        return list(map(int, list(word)))

    def next(item):
        if len(item) < maxlen:
            item.append(0)
            return item

        if len(item) == maxlen:
            if item[-1] == maxIndex:
                while item[-1] == maxIndex:
                    item.pop()
                item[-1] += 1
                return item

        item[-1] += 1
        return item

    now = [0]
    wordIntArr = wordToIntArr(word)
    for i in range(1, 10000):
        if now == wordIntArr:
            return i
        # print(now)
        now = next(now)

    return 0
profile
잘하고싶은사람

0개의 댓글