Skip to content

Commit a32273b

Browse files
committed
Solved the one with palindromes.
1 parent b8d5788 commit a32273b

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ add_subdirectory( kth-largest-element-in-a-array )
4848
add_subdirectory( wiggle-sort-ii )
4949
add_subdirectory( longest-palindrome )
5050
add_subdirectory( longest-palindrome-by-concatenating-two-letter-words )
51-
add_subdirectory( largest-palindromic-number )
51+
add_subdirectory( largest-palindromic-number )
52+
add_subdirectory( find-palindrome-with-fixed-length )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
project(find_palindrome_with_fixed_length)
2+
3+
add_executable(
4+
find_palindrome_with_fixed_length_runner
5+
runner.cpp
6+
)
7+
8+
add_executable(
9+
find_palindrome_with_fixed_length_tests
10+
tests.cpp
11+
)
12+
13+
target_link_libraries(
14+
find_palindrome_with_fixed_length_runner
15+
utils
16+
)
17+
18+
target_link_libraries(
19+
find_palindrome_with_fixed_length_tests
20+
utils
21+
GTest::gtest_main
22+
)
23+
24+
25+
gtest_discover_tests(find_palindrome_with_fixed_length_tests)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 2217. Find Palindrome With Fixed Length
2+
3+
[LeetCode link](https://leetcode.com/problems/find-palindrome-with-fixed-length/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include "print_vector.hpp"
5+
6+
#include "solution.hpp"
7+
8+
9+
int main()
10+
{
11+
const std::vector<int> input = { 1, 5 };
12+
const int k = 2;
13+
Solution s;
14+
std::cout << "\"" << input << "\": " << s.kthPalindrome(input, k) << std::endl;
15+
16+
return 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
class Solution {
7+
public:
8+
std::vector<long long> kthPalindrome(const std::vector<int>& queries, int intLength) {
9+
std::vector<long long> result(queries.size(), 0);
10+
11+
bool odd = intLength % 2 == 1;
12+
for (size_t i = 0; i < queries.size(); ++i) {
13+
result[i] = getNth(queries[i], odd, intLength);
14+
}
15+
16+
return result;
17+
}
18+
19+
long long getNth(int n, bool odd, int k) const {
20+
int multiplier = getMultiplier((k - 1) / 2);
21+
if (n > multiplier * 9) {
22+
return -1;
23+
}
24+
25+
int n2 = multiplier + n - 1;
26+
27+
int mid = -1;
28+
if (odd) {
29+
mid = n2 % 10;
30+
n2 = n2 / 10;
31+
}
32+
33+
return getNumber(n2, mid, k/2);
34+
}
35+
36+
int reversed(int input) const {
37+
int result = 0;
38+
while (input > 0) {
39+
result *= 10;
40+
result += (input % 10);
41+
input /= 10;
42+
}
43+
44+
return result;
45+
}
46+
public:
47+
long long getNumber(int input, int mid, int k) const {
48+
long long result = input;
49+
int multiplier = getMultiplier(k);
50+
51+
return (mid >= 0 ? (result * 10 + mid) : result) * multiplier + reversed(input);
52+
}
53+
54+
int getMultiplier(int k) const {
55+
int multiplier = 1;
56+
for (int i = 0; i < k; ++i) {
57+
multiplier *= 10;
58+
}
59+
60+
return multiplier;
61+
}
62+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <string>
2+
#include <utility>
3+
4+
#include "print_vector.hpp"
5+
6+
#include <gtest/gtest.h>
7+
8+
#include "solution.hpp"
9+
10+
typedef std::vector<int> vi;
11+
typedef std::vector<long long> vl;
12+
13+
class KThPalindromeTest :public ::testing::TestWithParam<std::tuple<vi, int, vl>> {
14+
protected:
15+
Solution solution;
16+
};
17+
18+
TEST_P(KThPalindromeTest, CheckSolution) {
19+
vi input = std::get<0>(GetParam());
20+
int k = std::get<1>(GetParam());
21+
vl expected = std::get<2>(GetParam());
22+
23+
vl actual = solution.kthPalindrome(input, k);
24+
25+
ASSERT_EQ(expected.size(), actual.size())
26+
<< "Expected " << expected << std::endl
27+
<<"Got " << actual;
28+
for (size_t i = 0; i < actual.size(); ++i) {
29+
ASSERT_EQ(expected[i], actual[i])
30+
<< "Difference at position " << i << ", for query " << input[i] << std::endl
31+
<< "Expected " << expected << std::endl << "Got " << actual;
32+
}
33+
}
34+
35+
INSTANTIATE_TEST_SUITE_P(
36+
KThPalindromeTests,
37+
KThPalindromeTest,
38+
::testing::Values(
39+
std::make_tuple<vi, int, vl>({ 1,2,3,4,5,90 }, 3, { 101,111,121,131,141,999 }),
40+
std::make_tuple<vi, int, vl>({ 2,4,6 }, 4, { 1111,1331,1551 }),
41+
std::make_tuple<vi, int, vl>({ 2,201429812,8,520498110,492711727,339882032,462074369,9,7,6 }, 1, { 2,-1,8,-1,-1,-1,-1,9,7,6 }),
42+
std::make_tuple<vi, int, vl>(
43+
{ 875,90,209,416,62,647,398,909,669,186,492,748,662,80,414,550,866,358,744,478,19,637,501,129,635,358,867,723,874,454,882,406,360,516,632,883,771,21,358,147,109,472,447,493903904,808,94,645,707,98043237,573,508,142,142,855,498,56,993,355,572,788,977,646,279,821,530,726,631,61,362,136,814,357,105,829909848,645,855,862,635,451,888,609788691,961592349,386,884,536,334,585,71,612 },
44+
15,
45+
{ 100008747800001,100000898000001,100002080200001,100004151400001,100000616000001,100006464600001,100003979300001,100009080900001,100006686600001,100001858100001,100004919400001,100007474700001,100006616600001,100000797000001,100004131400001,100005494500001,100008656800001,100003575300001,100007434700001,100004777400001,100000181000001,100006363600001,100005000500001,100001282100001,100006343600001,100003575300001,100008666800001,100007222700001,100008737800001,100004535400001,100008818800001,100004050400001,100003595300001,100005151500001,100006313600001,100008828800001,100007707700001,100000202000001,100003575300001,100001464100001,100001080100001,100004717400001,100004464400001,-1,100008070800001,100000939000001,100006444600001,100007060700001,-1,100005727500001,100005070500001,100001414100001,100001414100001,100008545800001,100004979400001,100000555000001,100009929900001,100003545300001,100005717500001,100007878700001,100009767900001,100006454600001,100002787200001,100008202800001,100005292500001,100007252700001,100006303600001,100000606000001,100003616300001,100001353100001,100008131800001,100003565300001,100001040100001,-1,100006444600001,100008545800001,100008616800001,100006343600001,100004505400001,100008878800001,-1,-1,100003858300001,100008838800001,100005353500001,100003333300001,100005848500001,100000707000001,100006111600001 })
46+
)
47+
);
48+
49+
class ReversedPalindromeTest :public ::testing::TestWithParam<std::tuple<int, int, int, int>> {
50+
protected:
51+
Solution solution;
52+
};
53+
54+
TEST_P(ReversedPalindromeTest, CheckSolution) {
55+
int input = std::get<0>(GetParam());
56+
int mid = std::get<1>(GetParam());
57+
int k = std::get<2>(GetParam());
58+
int expected = std::get<3>(GetParam());
59+
60+
auto actual = solution.getNumber(input, mid, k);
61+
62+
ASSERT_EQ(expected, actual);
63+
}
64+
65+
INSTANTIATE_TEST_SUITE_P(
66+
ReversedPalindromeTests,
67+
ReversedPalindromeTest,
68+
::testing::Values(
69+
std::make_tuple<int, int, int, int>(2, 1, 1, 212),
70+
std::make_tuple<int, int, int, int>(21, 3, 2, 21312),
71+
std::make_tuple<int, int, int, int>(21, -1, 2, 2112)
72+
)
73+
);
74+
75+
TEST(KThNumberTest, Check1) {
76+
Solution s;
77+
ASSERT_EQ(-1, s.getNth(98043237, true, 15));
78+
}

0 commit comments

Comments
 (0)