코딩테스트/프로그래머스

[프로그래머스] Level 2 - 영어 끝말잇기 (Python)

ye0nn 2022. 6. 24. 17:52

 

 

 

 

 

https://programmers.co.kr/learn/courses/30/lessons/12981

 

코딩테스트 연습 - 영어 끝말잇기

3 ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"] [3,3] 5 ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"] [0,0]

programmers.co.kr

 

 

 

 

 

✅ Solution

  • words를 돌면서 usedWords에 있거나 앞사람의 마지막 단어가 아닌 다른 단어로 시작했는지 검사하고 
    • 만약 그렇다면 몇번째 사람인지, 몇 번째인지 리턴한다.
    • 아니라면 usedWords에 word를 넣어준다.

 

 

 

 

✅ Code

import math

def solution(n, words):
    usedWords = []

    for idx, word in enumerate(words):
        if word in usedWords or (idx >= 1 and words[idx - 1][-1] != word[0]):
            return [idx % n + 1, math.ceil((idx + 1) / n)]

        usedWords.append(word)

    return [0, 0]