-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path173.cpp
57 lines (48 loc) · 1.15 KB
/
173.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
// A C++ solution for longest palindrome
#include <bits/stdc++.h>
using namespace std;
// Function to print a substring str[low..high]
void printSubStr(string str, int low, int high)
{
for (int i = low; i <= high; ++i)
cout << str[i];
}
// This function prints the
// longest palindrome substring
// It also returns the length
// of the longest palindrome
int longestPalSubstr(string str)
{
// get length of input string
int n = str.size();
// All substrings of length 1
// are palindromes
int maxLength = 1, start = 0;
// Nested loop to mark start and end index
for (int i = 0; i < str.length(); i++) {
for (int j = i; j < str.length(); j++) {
int flag = 1;
// Check palindrome
for (int k = 0; k < (j - i + 1) / 2; k++)
if (str[i + k] != str[j - k])
flag = 0;
// Palindrome
if (flag && (j - i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}
cout << "Longest palindrome substring is: ";
printSubStr(str, start, start + maxLength - 1);
// return length of LPS
return maxLength;
}
// Driver Code
int main()
{
string str = "forgeeksskeegfor";
cout << "\nLength is: "
<< longestPalSubstr(str);
return 0;
}