Skip to content

Commit b16d931

Browse files
committed
feat: add PROGRAMMERS-12904
1 parent 21cd4af commit b16d931

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

coding-test-cpp-practice/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(problem12904)
12
add_subdirectory(problem12939)
23
add_subdirectory(problem42883)
34
add_subdirectory(problem43105)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
enable_testing()
2+
find_package(GTest REQUIRED)
3+
4+
if(WIN32)
5+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
6+
endif(WIN32)
7+
8+
macro(add_library_target target)
9+
add_library(${target} STATIC "${target}.cpp")
10+
target_link_libraries(${target} PRIVATE ${ARGN})
11+
set_property(TARGET ${target} PROPERTY CXX_STANDARD 17)
12+
endmacro()
13+
14+
macro(add_target_test target)
15+
string(REPLACE "_test" "" target_name ${target})
16+
add_executable(${target} "${target}.cpp" "${target_name}.cpp")
17+
target_link_libraries(${target} PRIVATE GTest::gtest GTest::gtest_main ${ARGN})
18+
if(MSVC)
19+
### Edit and Continue for CMake projects
20+
target_compile_options(${target} PUBLIC "/Zi")
21+
target_link_options(${target} PUBLIC "/INCREMENTAL")
22+
endif()
23+
gtest_discover_tests(${target})
24+
endmacro()
25+
26+
add_library_target(longest_palindromic_substring)
27+
28+
add_target_test(longest_palindromic_substring_test)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "longest_palindromic_substring.h"
2+
3+
std::string LongestPalindromicSubstring::FindLongestPalindromicSubstring(const std::string& s)
4+
{
5+
auto longest = std::string{};
6+
for (auto i = 0; i < s.size(); ++i)
7+
{
8+
auto left = i;
9+
auto right = i;
10+
while (left >= 0 && right < s.size() && s[left] == s[right])
11+
{
12+
--left;
13+
++right;
14+
}
15+
if (longest.size() < right - left - 1)
16+
{
17+
longest = s.substr(left + 1, right - left - 1);
18+
}
19+
20+
left = i;
21+
right = i + 1;
22+
while (left >= 0 && right < s.size() && s[left] == s[right])
23+
{
24+
--left;
25+
++right;
26+
}
27+
if (longest.size() < right - left - 1)
28+
{
29+
longest = s.substr(left + 1, right - left - 1);
30+
}
31+
}
32+
return longest;
33+
}
34+
35+
int LongestPalindromicSubstring::FindLongestPalindromicSubstringLength(const std::string& s)
36+
{
37+
auto longest = 0;
38+
for (auto i = 0; i < s.size(); ++i)
39+
{
40+
auto left = i;
41+
auto right = i;
42+
while (left >= 0 && right < s.size() && s[left] == s[right])
43+
{
44+
--left;
45+
++right;
46+
}
47+
if (longest < right - left - 1)
48+
{
49+
longest = right - left - 1;
50+
}
51+
52+
left = i;
53+
right = i + 1;
54+
while (left >= 0 && right < s.size() && s[left] == s[right])
55+
{
56+
--left;
57+
++right;
58+
}
59+
if (longest < right - left - 1)
60+
{
61+
longest = right - left - 1;
62+
}
63+
}
64+
return longest;
65+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace LongestPalindromicSubstring
6+
{
7+
std::string FindLongestPalindromicSubstring(const std::string& s);
8+
int FindLongestPalindromicSubstringLength(const std::string& s);
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "longest_palindromic_substring.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
GTEST_TEST(FindLongestPalindromicSubstring, Successful)
6+
{
7+
const auto s = std::string{"babad"};
8+
const auto expected = std::string{"bab"};
9+
10+
const auto actual = LongestPalindromicSubstring::FindLongestPalindromicSubstring(s);
11+
EXPECT_EQ(expected, actual);
12+
}
13+
14+
GTEST_TEST(FindLongestPalindromicSubstringLength, Successful)
15+
{
16+
const auto s = std::string{"babad"};
17+
const auto expected = 3;
18+
19+
const auto actual = LongestPalindromicSubstring::FindLongestPalindromicSubstringLength(s);
20+
EXPECT_EQ(expected, actual);
21+
}

0 commit comments

Comments
 (0)