Given a string
Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
A palindrome is a string that reads the same forwards and backwards.
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
-
For example,
"ace" is a subsequence of"abcde" .
Output: 3
Explanation: There are 3 palindromic subsequences of length 3
- "aaa" subsequence of "aabca"
- "aba" subsequence of "aabca"
- "aba" subsequence of "aabca"
Output: 0
Explanation: There are no palindromic subsequences with length 3.
Constraints:
3 <= s.length <= 105 s consists of only lowercase English letters.
Contents
Solution 1 - Check possible 3 letter palindromes with 26 lowercase alphabets by maintaining left and right side characters
The main intution of this approach is that for any given character at
In this approach, we are going to use a
-
We will use
HashMap to collect each character from input strings to its count of how many the character have appeared, call itright . -
A
HashSet to keep track left side elements from current element as we visited through, call itleft . -
Another
HashSet to keep track of unique palindromes, call itunique .
Then, we will loop through each character from input string
-
First, we will decrement the element count from the
HashMap right , this is because we are going to consider this as centered element of palindrome and we are moving towards right, and if the count had become0 then we will remove it from map. -
Then, for each letter from
a toz , check if same letter appeared in bothleft HashSet andright HashMap , this means we have found a palindrome. So, add it tounique HashSet . -
At the end add current element to
left HashSet as we are moving towards right, and the current will become left side element for subsequent elements. -
At the end, return number of elements stored inside
unique HashSet , these are the unique palindromic subsequences with length 3.
Example:
Lets look at an example with
-
First, lets add the characters of input string
s into aHashMap (right ) with their count of how many times each letter have appeared, so the map will looks like this:Character Count a 3 b 1 c 1 HashSet (left ) to keep track of left side elements as we pass through. Initially it will be empty. -
Now, for each letter in input string
s , first will decrement the count from theright map, then from lettersa toz check if same letters is in bothleft as well asright .-
When we are at index
0 , we will decrement the count of'a' from the map, and since there are no letters on left side (left set is empty), there will not be any palindromes with length 3, so we will add current element to theleft and continue.
right HashMap andleft HashSet will look like this.right Character Count a 2 b 1 c 1 left Left character a
-
When we are at index
-
When we are at index
1 , we will decrement the count of'a' from the map, now theleft set has a lettera and the same letter present onright map as well, so a palindrome is found aaa, and we will add current element to theleft and continue.
right HashMap andleft HashSet will look like this.right Character Count a 1 b 1 c 1 left Left character a -
When we are at index
2 , we will decrement the count of'b' from the map, now theleft set has a lettera and the same letter present onright map as well, so a palindrome is found aba, and we will add current element to theleft and continue.
right HashMap andleft HashSet will look like this.right Character Count a 1 c 1 left Left character a b -
When we are at index
3 , we will decrement the count of'c' from the map, now theleft set has a lettera and the same letter present onright map as well, so a palindrome is found aca,left set has letter'b' but the same letter is not in theright map, so a palindrome is not possible with the letter 'b' from left. We will add current element to theleft and continue.
right HashMap andleft HashSet will look like this.right Character Count a 1 left Left character a b c -
When we are at index
4 , we will decrement the count of'a' from the map, now theleft set has letters, but theright map had become empty, so no palindromes are possible with length 3 at this index.
right HashMap andleft HashSet will look like this.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n)
Above implementations source code can be found at