![]() |
VOOZH | about |
Given a string S of lowercase letters, the task is to find the length of the longest palindromic subsequence made up of two distinct characters only.
Examples:
Input: S = "bbccdcbb"
Output: 7
Explanation:
The longest palindromic subsequence of the desired form is "bbcccbb", which is of length 7.
Input: S = "aeea"
Output: 4
Explanation:
The longest palindromic subsequence of desired form is "aeea", which is of length 4.
Approach:
In order to solve the problem, we need to follow the steps below:
Below is the implementation of the above approach:
7
Time Complexity: O(N), where N is the size of the given string.
Auxiliary Space: O(N), for creating 26 arrays of size N, the space complexity will be O(26*N) which is equivalent to O(N).