Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liubang committed Oct 7, 2024
1 parent 9b12024 commit d3ca8e7
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/49.group-anagrams.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2024 The Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Authors: liubang ([email protected])

#include <gtest/gtest.h>

#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>

namespace {
class Solution {
public:
std::vector<std::vector<std::string>> groupAnagrams(const std::vector<std::string>& strs) {
std::unordered_map<std::string, std::vector<std::string>> map;
for (const auto& str : strs) {
if (str.empty()) {
map[str].push_back(str);
} else {
std::string copy = str;
std::sort(copy.begin(), copy.end());
map[copy].push_back(str);
}
}
std::vector<std::vector<std::string>> result;
result.reserve(map.size());
for (const auto& [k, v] : map) {
result.emplace_back(v);
}
return result;
}
};
} // namespace

TEST(Leetcode, group_anagrams) {
Solution s;
{
std::vector<std::vector<std::string>> exp = {
{"bat"}, {"tan", "nat"}, {"eat", "tea", "ate"}};
EXPECT_EQ(exp, s.groupAnagrams({"eat", "tea", "tan", "ate", "nat", "bat"}));
}
{
std::vector<std::vector<std::string>> exp = {{""}};
EXPECT_EQ(exp, s.groupAnagrams({""}));
}
{
std::vector<std::vector<std::string>> exp = {{"a"}};
EXPECT_EQ(exp, s.groupAnagrams({"a"}));
}
}

0 comments on commit d3ca8e7

Please sign in to comment.