Given a string str, the task is to find the length of the longest palindromic subsequence of even length with no two adjacent characters same except the middle characters.
Input: str = “abscrcdba”
abccba is the required string which has no two consecutive characters same except the middle characters. Hence the length is 6
Input: str = “abcd”
Approach: The idea is to form a recursive solution and store the values of the subproblems using Dynamic Programming. The following steps can be followed to compute the result:
- Form a recursive function which will take a string and a character which is the starting character of the subsequence.
- If the first and last character of the string matches with the given character then remove the first and last character and call the function with all the character values from ‘a’ to ‘z’ except the given character, as the adjacent character cannot be same and find the maximum length.
- If the first and last character of the string does not match with the given character, then find the first and last index of the given character in the string, say i, j respectively. Take the substring from i to j and call the function with substring and the given character.
- Finally, memorise the values in an unordered map and use it if the function is again called with the same parameters.
The following is the implementation of the above approach:
Related Article: Longest Palindromic Subsequence
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.
- Longest Palindromic Subsequence of two distinct characters
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Longest subsequence with different adjacent characters
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Smallest Palindromic Subsequence of Even Length in Range [L, R]
- String with k distinct characters and no same characters adjacent
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Longest substring of vowels with no two adjacent alphabets same
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Replace '?' in a string such that no two adjacent characters are same
- Find the length of the longest subsequence with first K alphabets having same frequency
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Longest Palindromic Subsequence | DP-12
- Print Longest Palindromic Subsequence
- Length of longest increasing absolute even subsequence
- Longest Even Length Substring such that Sum of First and Second Half is same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.