Skip to content

Commit b8d5788

Browse files
committed
2384. Largest Palindromic Number
1 parent 11c3789 commit b8d5788

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ 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 )
50+
add_subdirectory( longest-palindrome-by-concatenating-two-letter-words )
51+
add_subdirectory( largest-palindromic-number )
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
project(largest_palindromic_number)
2+
3+
add_executable(
4+
largest_palindromic_number_runner
5+
runner.cpp
6+
)
7+
8+
add_executable(
9+
largest_palindromic_number_tests
10+
tests.cpp
11+
)
12+
13+
target_link_libraries(
14+
largest_palindromic_number_tests
15+
GTest::gtest_main
16+
)
17+
18+
19+
gtest_discover_tests(largest_palindromic_number_tests)

largest-palindromic-number/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 2384. Largest Palindromic Number
2+
3+
[LeetCode link](https://leetcode.com/problems/largest-palindromic-number/)

largest-palindromic-number/runner.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include "solution.hpp"
5+
6+
int main()
7+
{
8+
const std::string input = "112";
9+
Solution s;
10+
std::cout << "\"" << input << "\": " << s.largestPalindromic(input) << std::endl;
11+
12+
return 0;
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <algorithm>
4+
#include <cstring>
5+
#include <string>
6+
7+
class Solution {
8+
public:
9+
std::string largestPalindromic(const std::string& s) {
10+
int counts[10];
11+
std::memset(counts, 0, sizeof(int) * 10);
12+
for (auto c : s) {
13+
counts[c - '0']++;
14+
}
15+
16+
std::string result;
17+
bool nonzero = false;
18+
for (int i = 9; i > 0 || (i == 0 && nonzero); --i) {
19+
while (counts[i] > 1) {
20+
result += ('0' + i);
21+
counts[i] -= 2;
22+
nonzero = true;
23+
}
24+
}
25+
std::string mid;
26+
for (int i = 9; i >= 0; --i) {
27+
if (counts[i] != 0) {
28+
mid = '0' + i;
29+
break;
30+
}
31+
}
32+
33+
result += mid + std::string(result.rbegin(), result.rend());
34+
return result;
35+
}
36+
};

largest-palindromic-number/tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <string>
2+
#include <utility>
3+
4+
#include <gtest/gtest.h>
5+
6+
#include "solution.hpp"
7+
8+
class LargestPalindromicNumberTest : public ::testing::TestWithParam<std::pair<std::string, std::string>> {
9+
protected:
10+
Solution solution;
11+
};
12+
13+
TEST_P(LargestPalindromicNumberTest, CheckSolution) {
14+
std::string input = std::get<0>(GetParam());
15+
std::string expected = std::get<1>(GetParam());
16+
17+
auto actual = solution.largestPalindromic(input);
18+
19+
EXPECT_EQ(expected, actual);
20+
}
21+
22+
INSTANTIATE_TEST_SUITE_P(
23+
LargestPalindromicNumberTests,
24+
LargestPalindromicNumberTest,
25+
::testing::Values(
26+
std::make_pair<std::string, std::string>("1", "1"),
27+
std::make_pair<std::string, std::string>("12", "2"),
28+
std::make_pair<std::string, std::string>("112", "121"),
29+
std::make_pair<std::string, std::string>("1122", "2112"),
30+
std::make_pair<std::string, std::string>("100", "1"),
31+
std::make_pair<std::string, std::string>("00", "0"),
32+
std::make_pair<std::string, std::string>("110", "101")
33+
)
34+
);

0 commit comments

Comments
 (0)