Given a string, find the longest substring which is palindrome.
Input: Given string :"forgeeksskeegfor", Output: "geeksskeeg" Input: Given string :"Geeks", Output: "ee"
Method 1: Brute Force.
Approach: The simple approach is to check each substring whether the substring is a palindrome or not. To do this first, run three nested loops, the outer two loops pick all substrings one by one by fixing the corner characters, the inner loop checks whether the picked substring is palindrome or not.
- Time complexity: O(n^3).
Three nested loops are needed to find the longest palindromic substring in this approach, so the time complexity is O(n^3).
- Auxiliary complexity: O(1).
As no extra space is needed.
Method 2: Dynamic Programming.
Approach: The time complexity can be reduced by storing results of sub-problems. The idea is similar to this post.
- Maintain a boolean table[n][n] that is filled in bottom up manner.
- The value of table[i][j] is true, if the substring is palindrome, otherwise false.
- To calculate table[i][j], check the value of table[i+1][j-1], if the value is true and str[i] is same as str[j], then we make table[i][j] true.
- Otherwise, the value of table[i][j] is made false.
- We have to fill table previously for substring of length = 1 and length =2 because
as we are finding , if table[i+1][j-1] is true or false , so in case of
(i) length == 1 , lets say i=2 , j=2 and i+1,j-1 doesn’t lies between [i , j]
(ii) length == 2 ,lets say i=2 , j=3 and i+1,j-1 again doesn’t lies between [i , j].
Below is the implementation of the above approach:
Longest palindrome substring is: geeksskeeg Length is: 10
- Time complexity: O(n^2).
Two nested traversals are needed.
- Auxiliary Space: O(n^2).
Matrix of size n*n is needed to store the dp array.
A better space complexity approach can be found in Set-2.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum length of substring whose rotation generates a palindromic substring
- Longest Palindromic Substring | Set 2
- Longest Non-palindromic substring
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Longest palindromic string possible after removal of a substring
- Rearrange string to obtain Longest Palindromic Substring
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Maximum length palindromic substring such that it starts and ends with given char
- Shortest Palindromic Substring
- Minimum size substring to be removed to make a given string palindromic
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest substring with atmost K characters from the given set of characters
- Longest Palindromic Subsequence | DP-12