-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestPalindrome.cpp
62 lines (55 loc) · 1.05 KB
/
longestPalindrome.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
#include <bits/stdc++.h>
using namespace std;
string isPalindrome(int i, int j, string s) {
int ct = 0;
string s1 = "";
string s2 = "";
int n = s.size();
if (i == j) {
s2 += s[j];
ct++;
i--;
j++;
}
while (i >= 0 && j < n && s[i] == s[j]) {
s1 += s[i];
s2 += s[j];
ct += 2;
i--;
j++;
}
reverse(s1.begin(), s1.end());
return s1 + s2;
}
string longestPalindrome(string s) {
int len = -1;
int n = s.size();
string ans = "";
string ans1 = "";
string ans2 = "";
int k = 0;
for (int i = 0; i < n; i++) {
ans1 = isPalindrome(i, i, s);
//cout<<ans1<<endl;
k = ans1.size();
if (k > len) {
ans = ans1;
len = k;
}
if (i < (n - 1)) {
ans2 = isPalindrome(i, i + 1, s);
//cout<<ans2<<endl;
k = ans2.size();
if (k > len) {
ans = ans2;
len = k;
}
}
}
//cout<<ans<<endl;
return ans;
}
int main() {
string s = "babad";
cout << longestPalindrome(s) << endl;
}