Longest Palindromic Substring in C++

Program


#include 
#include 
#include 

using namespace std;

/**
 * Function to expand around the center and find the longest palindrome.
 * @param s The input string.
 * @param left The left index of the center.
 * @param right The right index of the center.
 * @return The longest palindromic substring centered at (left, right).
 */
string expandAroundCenter(const string& s, int left, int right) {
    while (left >= 0 && right < s.size() && s[left] == s[right]) {
        --left;
        ++right;
    }
    return s.substr(left + 1, right - left - 1);
}

/**
 * Function to find the longest palindromic substring.
 * @param s The input string.
 * @return The longest palindromic substring in the input string.
 */
string longestPalindrome(const string& s) {
    if (s.empty()) return "";

    string longest;
    for (int i = 0; i < s.size(); ++i) { // Odd-length palindromes string oddPalindrome = expandAroundCenter(s, i, i); if (oddPalindrome.size() > longest.size()) {
            longest = oddPalindrome;
        }

        // Even-length palindromes
        string evenPalindrome = expandAroundCenter(s, i, i + 1);
        if (evenPalindrome.size() > longest.size()) {
            longest = evenPalindrome;
        }
    }
    return longest;
}

int main() {
    string input;
    cout << "Enter a string: "; cin >> input;

    string result = longestPalindrome(input);
    cout << "Longest palindromic substring: " << result << endl;

    return 0;
}
    

Explanation

The provided C++ program finds the longest palindromic substring in a given string using the “Expand Around Center” approach. This approach is efficient and works as follows:

  1. Function: expandAroundCenter
    • This function expands around the center of a potential palindrome. It takes two indices, left and right, and keeps expanding outward as long as the characters at these indices are equal.
    • When expansion is no longer possible, it returns the longest palindromic substring found within the given indices.
  2. Function: longestPalindrome
    • This function iterates through each character of the input string and treats it as the center of a palindrome.
    • It checks for both odd-length and even-length palindromes by calling expandAroundCenter with appropriate center indices.
    • It keeps track of the longest palindromic substring found and returns it as the result.
  3. Main Function
    • It reads a string from the user, calls the longestPalindrome function, and prints the result.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)