-
Notifications
You must be signed in to change notification settings - Fork 0
feat: solve 49. Group Anagrams with unit testing #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| problem: | ||
| number: 49 | ||
| title: "Group Anagrams" | ||
| leetcode_url: "https://leetcode.com/problems/group-anagrams/" | ||
| difficulty: "medium" | ||
| tags: ["Arrays & Hashing"] | ||
|
|
||
| solutions: | ||
| python: "problems/group_anagrams/group_anagrams.py" | ||
| cpp: "problems/group_anagrams/group_anagrams.cpp" | ||
|
mathusanm6 marked this conversation as resolved.
Outdated
|
||
|
|
||
| complexity: | ||
| time: "O(n * k log k)" | ||
| space: "O(n)" | ||
|
|
||
| notes: "For C++, the complexity is _O(n * k log k)_, where n is the number of strings and k is the maximum length of a string. But for Python, the complexity is _O(n * k)_ as there is no sorting involved." | ||
| readme_link: "" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #include "group_anagrams.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <ranges> // NOLINT(misc-include-cleaner) needed for std::ranges::sort | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| vector<vector<string>> groupAnagrams(vector<string>& strs) { | ||
| unordered_map<string, vector<string>> groups; | ||
| groups.reserve(strs.size()); | ||
|
|
||
| for (const string& s : strs) { | ||
| string key = s; | ||
| std::ranges::sort(key); // anagrams share the same sorted key | ||
| groups[key].push_back(s); | ||
| } | ||
|
|
||
| vector<vector<string>> res; | ||
| res.reserve(groups.size()); | ||
| for (auto& [k, v] : groups) { | ||
| res.push_back(std::move(v)); | ||
| } | ||
| return res; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string>& strs); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import collections | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| def groupAnagrams(strs: List[str]) -> List[List[str]]: | ||
| groups = collections.defaultdict(list) | ||
| for s in strs: | ||
| count = [0] * 26 | ||
| for c in s: | ||
| count[ord(c) - ord("a")] += 1 | ||
| groups[tuple(count)].append(s) | ||
| return list(groups.values()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "group_anagrams.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <algorithm> | ||
| #include <ranges> // NOLINT(misc-include-cleaner) needed for std::ranges::sort | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| struct GroupAnagramsCase { | ||
| std::string test_name; | ||
| std::vector<std::string> strs; | ||
| std::vector<std::vector<std::string>> expected; | ||
| }; | ||
|
|
||
| using GroupAnagramsTest = ::testing::TestWithParam<GroupAnagramsCase>; | ||
|
|
||
| namespace { | ||
| // Helper function to sort and compare results | ||
| bool compareResults(std::vector<std::vector<std::string>> result, | ||
| std::vector<std::vector<std::string>> expected) { | ||
| // Sort inner vectors and outer vector for comparison | ||
| for (auto& group : result) { | ||
| std::ranges::sort(group); | ||
| } | ||
| std::ranges::sort(result); | ||
|
|
||
| for (auto& group : expected) { | ||
| std::ranges::sort(group); | ||
| } | ||
| std::ranges::sort(expected); | ||
|
|
||
| return result == expected; | ||
| } | ||
| } // namespace | ||
|
|
||
| TEST_P(GroupAnagramsTest, TestCases) { | ||
| const GroupAnagramsCase& testCase = GetParam(); | ||
| std::vector<std::string> input = testCase.strs; // Make a copy since function might modify | ||
| const std::vector<std::vector<std::string>> result = groupAnagrams(input); | ||
| EXPECT_TRUE(compareResults(result, testCase.expected)); | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P( | ||
| GroupAnagramsTestCases, GroupAnagramsTest, | ||
| ::testing::Values( | ||
| GroupAnagramsCase{.test_name = "BasicCase", | ||
| .strs = {"eat", "tea", "tan", "ate", "nat", "bat"}, | ||
| .expected = {{"bat"}, {"tan", "nat"}, {"eat", "tea", "ate"}}}, | ||
| GroupAnagramsCase{.test_name = "SingleEmptyString", .strs = {""}, .expected = {{""}}}, | ||
| GroupAnagramsCase{.test_name = "SingleCharacter", .strs = {"a"}, .expected = {{"a"}}}, | ||
| GroupAnagramsCase{.test_name = "MultipleAnagramGroups", | ||
| .strs = {"abc", "bca", "cab", "xyz", "zyx", "yxz"}, | ||
| .expected = {{"abc", "bca", "cab"}, {"xyz", "zyx", "yxz"}}}, | ||
| GroupAnagramsCase{ | ||
| .test_name = "LongerStrings", | ||
| .strs = {"listen", "silent", "enlist", "inlets", "google", "gogole"}, | ||
| .expected = {{"listen", "silent", "enlist", "inlets"}, {"google", "gogole"}}}, | ||
| GroupAnagramsCase{.test_name = "AllAnagrams", | ||
| .strs = {"aabb", "abab", "bbaa", "baba", "abba"}, | ||
| .expected = {{"aabb", "abab", "bbaa", "baba", "abba"}}}, | ||
| GroupAnagramsCase{.test_name = "MixedLengths", | ||
| .strs = {"rat", "tar", "art", "star", "tars"}, | ||
| .expected = {{"rat", "tar", "art"}, {"star", "tars"}}}, | ||
| GroupAnagramsCase{.test_name = "EmptyList", .strs = {}, .expected = {}}), | ||
| [](const ::testing::TestParamInfo<GroupAnagramsCase>& info) { return info.param.test_name; }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Test cases for the group_anagrams function.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from group_anagrams import groupAnagrams | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "strs, expected", | ||
| [ | ||
| ( | ||
| ["eat", "tea", "tan", "ate", "nat", "bat"], | ||
| [["bat"], ["tan", "nat"], ["eat", "tea", "ate"]], | ||
| ), # Basic case | ||
| ([""], [[""]]), # Single empty string | ||
| (["a"], [["a"]]), # Single character | ||
| ( | ||
| ["abc", "bca", "cab", "xyz", "zyx", "yxz"], | ||
| [["abc", "bca", "cab"], ["xyz", "zyx", "yxz"]], | ||
| ), # Multiple anagram groups | ||
| ( | ||
| ["listen", "silent", "enlist", "inlets", "google", "gogole"], | ||
| [["listen", "silent", "enlist", "inlets"], ["google", "gogole"]], | ||
| ), # Longer strings | ||
| ( | ||
| ["aabb", "abab", "bbaa", "baba", "abba"], | ||
| [["aabb", "abab", "bbaa", "baba", "abba"]], | ||
| ), # All are anagrams | ||
| ( | ||
| ["rat", "tar", "art", "star", "tars"], | ||
| [["rat", "tar", "art"], ["star", "tars"]], | ||
| ), # Mixed lengths | ||
| ([], []), # Empty list | ||
| ], | ||
| ids=[ | ||
| "basic_case", | ||
| "single_empty_string", | ||
| "single_character", | ||
| "multiple_anagram_groups", | ||
| "longer_strings", | ||
| "all_anagrams", | ||
| "mixed_lengths", | ||
| "empty_list", | ||
| ], | ||
| ) | ||
| def test_group_anagrams(strs, expected): | ||
| result = groupAnagrams(strs) | ||
| # Sort inner lists and the outer list for comparison | ||
| result = [sorted(group) for group in result] | ||
| result.sort() | ||
| expected = [sorted(group) for group in expected] | ||
| expected.sort() | ||
| assert result == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.