-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.cpp
98 lines (90 loc) · 2.06 KB
/
5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <gtest/gtest.h>
#include <string>
using namespace std;
class Solution {
public:
string longestPalindrome(string s)
{
int longest = 0;
int start = 0;
int i = 0;
while (s[i] != '\0') {
int exp = 0;
while (i - exp > -1 && s[i + exp] != '\0') {
if (s[i - exp] == s[i + exp]) {
exp++;
}
else {
break;
}
}
exp -= 1;
if (exp * 2 + 1 > longest) {
longest = exp * 2 + 1;
start = i - exp;
}
exp = 0;
while (i - exp > -1 && s[i + 1 + exp] != '\0') {
if (s[i - exp] == s[i + 1 + exp]) {
exp++;
}
else {
break;
}
}
exp -= 1;
if (exp * 2 + 2 > longest) {
longest = exp * 2 + 2;
start = i - exp;
}
i++;
}
return s.substr(start, longest);
}
};
class Testing : public testing::Test {
public:
Solution s;
};
TEST_F(Testing, Case1)
{
string input = "babad";
string output = "bab";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case2)
{
string input = "cbbd";
string output = "bb";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case3)
{
string input = "a";
string output = "a";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case4)
{
string input = "ac";
string output = "a";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case5)
{
string input = "bb";
string output = "bb";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case6)
{
string input = "ccc";
string output = "ccc";
EXPECT_EQ(s.longestPalindrome(input), output);
}
TEST_F(Testing, Case7)
{
string input = "aaaa";
string output = "aaaa";
EXPECT_EQ(s.longestPalindrome(input), output);
}