All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0049_group_anagrams.Solution.cpp Maven / Gradle / Ivy

The newest version!
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2024_05_25_Time_18_ms_(96.16%)_Space_22.7_MB_(93.37%)

#include 
#include 
#include 
#include 

class Solution {
public:
    std::vector> groupAnagrams(std::vector& strs) {
        std::unordered_map> hm;
        for (const auto& s : strs) {
            std::string temp = s;
            std::sort(temp.begin(), temp.end());
            hm[temp].push_back(s);
        }
        std::vector> result;
        for (auto& pair : hm) {
            result.push_back(std::move(pair.second));
        }
        return result;
    }
};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy