Skip to content

Commit 11c3789

Browse files
committed
2131
1 parent 13ab646 commit 11c3789

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ add_subdirectory( kth-largest-element-in-a-stream )
4747
add_subdirectory( kth-largest-element-in-a-array )
4848
add_subdirectory( wiggle-sort-ii )
4949
add_subdirectory( longest-palindrome )
50+
add_subdirectory( longest-palindrome-by-concatenating-two-letter-words )
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
project(longest_palindrome_2131)
2+
3+
add_executable(
4+
longest_palindrome_2131_runner
5+
runner.cpp
6+
)
7+
8+
add_executable(
9+
longest_palindrome_2131_tests
10+
tests.cpp
11+
)
12+
13+
target_link_libraries(
14+
longest_palindrome_2131_runner
15+
utils
16+
)
17+
18+
19+
target_link_libraries(
20+
longest_palindrome_2131_tests
21+
GTest::gtest_main
22+
)
23+
24+
25+
gtest_discover_tests(longest_palindrome_2131_tests)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 2131. Longest Palindrome by Concatenating Two Letter Words
2+
3+
[LeetCode link](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "print_vector.hpp"
6+
7+
#include "solution.hpp"
8+
9+
int main()
10+
{
11+
const std::vector<std::string> input = { "aa", "bb", "ab", "bc", "bb"};
12+
Solution s;
13+
std::cout << input << ": " << s.longestPalindrome(input) << std::endl;
14+
15+
return 0;
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <unordered_map>
5+
#include <unordered_set>
6+
#include <vector>
7+
8+
class Solution {
9+
public:
10+
int longestPalindrome(const std::vector<std::string>& words) {
11+
std::unordered_map<std::string, int> known;
12+
std::unordered_set<std::string> same;
13+
int result = 0;
14+
bool odd = false;
15+
for (auto c : words) {
16+
auto it = known.find(c);
17+
if (it != known.end()) {
18+
result += 4;
19+
it->second--;
20+
if (it->second == 0) {
21+
known.erase(it);
22+
same.erase(c);
23+
}
24+
}
25+
else {
26+
std::string reversed(c.rbegin(), c.rend());
27+
known[reversed]++;
28+
if (c[0] == c[1]) {
29+
same.insert(c);
30+
}
31+
}
32+
}
33+
34+
if (!same.empty()) {
35+
result += 2;
36+
}
37+
38+
return result;
39+
}
40+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <string>
4+
#include <utility>
5+
6+
#include "solution.hpp"
7+
8+
class LongestPalindromeTest :public ::testing::TestWithParam<std::pair<std::vector<std::string>, int>> {
9+
protected:
10+
Solution solution;
11+
};
12+
13+
TEST_P(LongestPalindromeTest, CheckSolution) {
14+
std::vector<std::string> input = std::get<0>(GetParam());
15+
int expected = std::get<1>(GetParam());
16+
17+
int actual = solution.longestPalindrome(input);
18+
19+
EXPECT_EQ(expected, actual);
20+
}
21+
22+
INSTANTIATE_TEST_SUITE_P(
23+
LongestPalindromeTests,
24+
LongestPalindromeTest,
25+
::testing::Values(
26+
std::make_pair<std::vector<std::string>, int>({ "aa" }, 2),
27+
std::make_pair<std::vector<std::string>, int>({ "aa", "ab"}, 2),
28+
std::make_pair<std::vector<std::string>, int>({ "ab", "ba"}, 4),
29+
std::make_pair<std::vector<std::string>, int>({ "ab", "ba", "aa"}, 6),
30+
std::make_pair<std::vector<std::string>, int>({ "qo","fo","fq","qf","fo","ff","qq","qf","of","of","oo","of","of","qf","qf","of" }, 14)
31+
)
32+
);

0 commit comments

Comments
 (0)