Skip to content

Commit

Permalink
#289 added longestpalindromic substring (#291)
Browse files Browse the repository at this point in the history
* added longestpalindromic substring

* added longestpalindromic substring

---------

Co-authored-by: albinsabu2023 <[email protected]>
  • Loading branch information
albinsabu2023 and albinsabu2023 authored Oct 4, 2024
1 parent 9d4d6b0 commit 7ef36aa
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions C++/LongestPalindromicSubstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <string>
using namespace std;
/**
* @brief
* author @albinsabu2023
* time complexity O(n*n)
* space complexity O(n)
*
* approach:expand around index
* two cases
* odd length palindrome (single center index)
* even length palindrome (two center index)
*
*/
class Solution
{
public:
// Function to expand around the center and find the palindrome
string expand(string s, int left, int right)
{
int n = s.length();
while (left >= 0 && right < n && s[left] == s[right])
{
left--;
right++;
}
return s.substr(left + 1, right - left - 1); // return the palindromic substring
}

// Function to find the longest palindromic substring
string longestPalindrome(string s)
{
if (s.length() == 1)
return s; // if the string has only one character, it's the longest palindrome
int n = s.length();
string ans = ""; // variable to store the longest palindrome

for (int i = 0; i < n - 1; i++)
{
// Check for odd-length palindromes (centered at one character)
string oddlength = expand(s, i, i);
if (oddlength.length() > ans.length())
{
ans = oddlength;
}

// Check for even-length palindromes (centered between two characters)
string evenlength = expand(s, i, i + 1);
if (evenlength.length() > ans.length())
{
ans = evenlength;
}
}

return ans;
}
};

int main()
{
Solution sol;
string s;
cout << "Enter the string: ";
cin >> s;

string result = sol.longestPalindrome(s);
cout << "Longest palindromic substring is: " << result << endl;

return 0;
}

0 comments on commit 7ef36aa

Please sign in to comment.