From a4ebb5a0a1b08111f898ea5518ae5625a6d62f17 Mon Sep 17 00:00:00 2001 From: albinsabu2023 Date: Fri, 4 Oct 2024 03:16:00 +0530 Subject: [PATCH 1/2] added longestpalindromic substring --- C++/LongestPalindromicSubstring.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 C++/LongestPalindromicSubstring.cpp diff --git a/C++/LongestPalindromicSubstring.cpp b/C++/LongestPalindromicSubstring.cpp new file mode 100644 index 0000000..e69de29 From 44e4ba514adde67baa0916e16547b5f4009a6bff Mon Sep 17 00:00:00 2001 From: albinsabu2023 Date: Fri, 4 Oct 2024 03:20:40 +0530 Subject: [PATCH 2/2] added longestpalindromic substring --- C++/LongestPalindromicSubstring.cpp | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/C++/LongestPalindromicSubstring.cpp b/C++/LongestPalindromicSubstring.cpp index e69de29..8cdc79f 100644 --- a/C++/LongestPalindromicSubstring.cpp +++ b/C++/LongestPalindromicSubstring.cpp @@ -0,0 +1,71 @@ +#include +#include +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; +}