Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create LongestPalindromeSubstring.cpp #302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Cpp/LongestPalindromeSubstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// A O(n^2) time and O(1) space program to
// find the longest palindromic substring
// easy to understand as compared to previous version.
#include <bits/stdc++.h>
using namespace std;

// A utility function to print
// a substring str[low..high]
// This function prints the
// longest palindrome substring (LPS)
// of str[]. It also returns the
// length of the longest palindrome
int longestPalSubstr(string str)
{
int n = str.size(); // calculating size of string
if (n < 2)
return n; // if string is empty then size will be 0.
// if n==1 then, answer will be 1(single
// character will always palindrome)

int maxLength = 1, start = 0;
int low, high;
for (int i = 0; i < n; i++) {
low = i - 1;
high = i + 1;
while (high < n
&& str[high] == str[i]) // increment 'high'
high++;

while (low >= 0
&& str[low] == str[i]) // decrement 'low'
low--;

while (low >= 0 && high < n
&& str[low] == str[high]) {
low--;
high++;
}

int length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}

cout << "Longest palindrome substring is: ";
cout << str.substr(start, maxLength);
return maxLength;
}

// Driver program to test above functions
int main()
{
string str = "forgeeksskeegfor";
cout << "\nLength is: " << longestPalSubstr(str)
<< endl;
return 0;
}