프로그래머스 142086 가장 가까운 같은 글자

1 개요[ | ]

프로그래머스 142086 가장 가까운 같은 글자

2 C++[ | ]

#include <string>
#include <vector>
#include <map>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    map<char,int> m;
    for(int i=0; i<s.length(); i++) {
        char ch = s[i];
        map<char,int>::iterator it = m.find(ch);
        if(it == m.end()) {
            answer.push_back(-1);
        } else {
            answer.push_back(i-it->second);
        }
        m[ch] = i;
    }
    return answer;
}
#include <string>
#include <vector>
#include <map>
using namespace std;

vector<int> solution(string s) {
    vector<int> answer;
    map<char,int> m;
    for(int i=0; i<s.length(); i++) {
        char ch = s[i];
        if(m.find(ch) == m.end()) {
            answer.push_back(-1);
        } else {
            answer.push_back(i-m[ch]);
        }
        m[ch] = i;
    }
    return answer;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}