본문 바로가기
[알고리즘 + 자료구조]/[프로그래머스]

프로그래머스 영어 끝말잇기

by Hevton 2023. 8. 8.
반응형

 

 

다른분들과 풀이가 같다.

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

using namespace std;

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

    return {0, 0};
}

 

종료조건은

1. 이미 등장한 단어이거나

2. 이전 단어와 끝말잇기가 아닐경우

 

두가지 중 하나에 만족하면 된다.

반응형