Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
771 B
2
Indexable
Never
vector<vector<string>> getStringAnagramWithHighestCount(vector<string> strs) {
    unordered_map<string, vector<string>> anagramGroups;
    int maxCount = 0;

    // Group anagrams based on sorted strings
    for (const string& str : strs) {
        string sortedStr = str;
        sort(sortedStr.begin(), sortedStr.end());
        anagramGroups[sortedStr].push_back(str);
        maxCount = max(maxCount, static_cast<int>(anagramGroups[sortedStr].size()));
    }

    // Collect anagram groups with highest count
    vector<vector<string>> result;
    for (auto it = anagramGroups.begin(); it != anagramGroups.end(); ++it) {
        if (it->second.size() == maxCount) {
            result.push_back(it->second);
        }
    }

    return result;
}
Leave a Comment