궁금한게 많은 개발자 노트

[ leetcode ] 49. Group Anagrams 본문

카테고리 없음

[ leetcode ] 49. Group Anagrams

궁금한게 많은 개발자 2023. 5. 2. 11:51

string의 배열이 들어왔을 때, anagram 단어들을 grouping하여 반환하는 문제입니다.

Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once
#include<algorithm>
#include<unordered_map>

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> hash_table;
        for (int i = 0; i < strs.size(); i++){
            string temp = strs[i];
            sort(temp.begin(), temp.end());
            hash_table[temp].push_back(strs[i]);
        }
        
        vector<vector<string>> answer;
        for (pair<string, vector<string>> item : hash_table)
      		answer.push_back(item.second);
        return answer;
    }
};

 

 

 

Comments