Solved: 27 - Unordered Containers
This commit is contained in:
@@ -1,21 +1,37 @@
|
|||||||
#include "unordered_containers.hpp"
|
#include "unordered_containers.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
namespace stl_containers {
|
namespace stl_containers {
|
||||||
|
|
||||||
auto first_unique(const std::vector<std::string>& words) -> std::string {
|
auto first_unique(const std::vector<std::string>& words) -> std::string {
|
||||||
(void)words;
|
auto s = std::set<std::string>();
|
||||||
return {};
|
for (const auto& word : words) {
|
||||||
|
if (!s.contains(word)) {
|
||||||
|
s.insert(word);
|
||||||
|
} else {
|
||||||
|
s.erase(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.empty() ? "" : *s.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto group_anagrams(const std::vector<std::string>& words)
|
auto group_anagrams(const std::vector<std::string>& words)
|
||||||
-> std::unordered_map<std::string, std::vector<std::string>> {
|
-> std::unordered_map<std::string, std::vector<std::string>> {
|
||||||
(void)words;
|
auto groups = std::unordered_map<std::string, std::vector<std::string>>();
|
||||||
return {};
|
for (const auto& word : words) {
|
||||||
|
auto key = word;
|
||||||
|
std::ranges::sort(key);
|
||||||
|
groups[key].push_back(word);
|
||||||
|
}
|
||||||
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto has_duplicate(const std::vector<int>& data) -> bool {
|
auto has_duplicate(const std::vector<int>& data) -> bool {
|
||||||
(void)data;
|
auto s = std::set<int>{};
|
||||||
return false;
|
for (auto i : data) s.insert(i);
|
||||||
|
return s.size() != data.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace stl_containers
|
} // namespace stl_containers
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "unordered_containers.hpp"
|
#include "unordered_containers.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ranges>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(UnorderedContainers, FirstUnique) {
|
TEST(UnorderedContainers, FirstUnique) {
|
||||||
@@ -9,3 +12,42 @@ TEST(UnorderedContainers, HasDuplicate) {
|
|||||||
EXPECT_TRUE(stl_containers::has_duplicate({1, 2, 2}));
|
EXPECT_TRUE(stl_containers::has_duplicate({1, 2, 2}));
|
||||||
EXPECT_FALSE(stl_containers::has_duplicate({1, 2, 3}));
|
EXPECT_FALSE(stl_containers::has_duplicate({1, 2, 3}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(UnorderedContainers, GroupAnagramsBasic) {
|
||||||
|
auto result = stl_containers::group_anagrams({"eat", "tea", "tan", "ate", "nat", "bat"});
|
||||||
|
// Sort each group for deterministic comparison
|
||||||
|
for (auto &group: result | std::views::values) {
|
||||||
|
std::ranges::sort(group);
|
||||||
|
}
|
||||||
|
ASSERT_EQ(result.size(), 3);
|
||||||
|
EXPECT_EQ(result["aet"], (std::vector<std::string>{"ate", "eat", "tea"}));
|
||||||
|
EXPECT_EQ(result["ant"], (std::vector<std::string>{"nat", "tan"}));
|
||||||
|
EXPECT_EQ(result["abt"], (std::vector<std::string>{"bat"}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnorderedContainers, GroupAnagramsEmpty) {
|
||||||
|
auto result = stl_containers::group_anagrams({});
|
||||||
|
EXPECT_TRUE(result.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnorderedContainers, GroupAnagramsSingleWord) {
|
||||||
|
auto result = stl_containers::group_anagrams({"hello"});
|
||||||
|
ASSERT_EQ(result.size(), 1);
|
||||||
|
EXPECT_EQ(result["ehllo"], (std::vector<std::string>{"hello"}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnorderedContainers, GroupAnagramsNoAnagrams) {
|
||||||
|
auto result = stl_containers::group_anagrams({"abc", "def", "ghi"});
|
||||||
|
ASSERT_EQ(result.size(), 3);
|
||||||
|
EXPECT_EQ(result["abc"].size(), 1);
|
||||||
|
EXPECT_EQ(result["def"].size(), 1);
|
||||||
|
EXPECT_EQ(result["ghi"].size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UnorderedContainers, GroupAnagramsAllSame) {
|
||||||
|
auto result = stl_containers::group_anagrams({"abc", "bca", "cab"});
|
||||||
|
ASSERT_EQ(result.size(), 1);
|
||||||
|
auto& group = result["abc"];
|
||||||
|
std::ranges::sort(group);
|
||||||
|
EXPECT_EQ(group, (std::vector<std::string>{"abc", "bca", "cab"}));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user