[프로그래머스/C++]Lv.0 - 한 번만 등장한 문자

YH J·2023년 4월 17일
0

프로그래머스

목록 보기
17/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120896

내 풀이

map을 이용하여 글자 하나하나 map에 추가한다. 중복되면 value값을 ++한다.
다 추가 한 뒤 value가 1이면 해당 문자를 answer에 넣어준다

내 코드

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

string solution(string s) {
    string answer = "";

    map<char,int> m;
    for(const auto v : s)
    {
        m[v]++;
    }

    for(const auto& v : m)
    {
        if(v.second == 1)
        {
            answer.push_back(v.first);
        }
    }
    return answer;
}

다른 사람의 풀이

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

using namespace std;

string solution(string s) {
    string answer = "";
    sort(s.begin(),s.end());
    map<char,int> m;
    for(auto ch : s)
    {
        m[ch]++;
    }
    for(auto it= m.begin(); it != m.end(); it++)
    {
        if(it->second == 1)
            answer.push_back(it->first);
    }
    sort(answer.begin(),answer.end());
    return answer;
}

다른 사람의 풀이 해석

map의 for문을 돌릴 때 iterator를 사용하였다.

profile
게임 개발자 지망생

0개의 댓글