본문 바로가기
coding test

[프로그래머스/level2] 영어 끝말잇기

by objet 2024. 1. 17.

 

문제

 

 

풀이과정

원래는 vector만을 이용해서 풀었는데 map을 사용해서 key로 영단어들을 처리하는 게 더 효율적임을 깨닫고 한 번 코드를 엎었다.

 

 

풀이 코드

#include <string>
#include <vector>
#include <map>

using namespace std;

vector<int> solution(int n, vector<string> words) {
    map<string, int> word;
    word[words[0]] = 1;
    
    for (int i = 1; i < words.size(); i++) {
        if (word[words[i]] || words[i - 1].back() != words[i].front())
            return {(i % n) + 1, (i / n) + 1};
        word[words[i]] = 1;
    }

    return {0, 0};
}

 

 

실행 결과