Problem
Given a string s
, return the number of unique palindromes of length three that are a subsequence of s
.
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"
.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Constraints
3 <= s.length <= 105
s
consists of only lowercase English letters.
Solution
Video explanation
Here is the video explaining this method in detail. Please check it out:
Method 1 - Brute Force
We need set to store the result, as subsequences must be unique.
To solve the problem of finding the number of unique palindromes of length three that are subsequences of a given string s
, we can follow these steps:
- Identify Potential Palindromes: All palindromes of length 3 are of the form “aba”, “cac”, etc. This means the first and third character must be the same.
- Track Occurrences: Use a set data structure to keep track of unique palindromic subsequences.
- Traverse the String: Use three nested loops to traverse the string and check all combinations of triplets that could form such palindromic patterns.
- Check and Store: For each triplet (i, j, k), check if it forms a palindrome, and if so, store it in the set.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^3)
- 🧺 Space complexity:
O(26^2) =
O(1)`
Method 2 - Using Input Constraint of Only Letters [a-z]
Allowed
Here are points to note:
- Character Constraints: Since we only have 26 possible characters (
a
toz
), this problem can be solved inO(26*n)
ORO(n)
. - Palindrome Structure: For a palindrome of length three like
aba
,aca
, etc., the first and last characters must be the same. - First and Last Occurrences: By finding the first and last occurrences of each character, you can identify the range in which potential palindromes might exist.
- So, we find first and last occurence of each character and store it in hashmap or in this case in 26 char arrays -
firstIndex
andlastIndex
- The, we will loop from characters
a
toz
, and for each character from ‘a’ to ‘z’, if it appears more than once (i.e.,firstIdx[i] < lastIdx[i]
), count the distinct characters between its first and last occurrences.
- So, we find first and last occurence of each character and store it in hashmap or in this case in 26 char arrays -
Example: abcbba
, we have two unique chars between first and last a
(c
and b
), and two - between first and last b
(b
and c
). No characters in between c
so it forms no palindromes.
- First occurrence of ‘a’: index 0, Last occurrence: index 5
- Characters between first and last occurrence of ‘a’:
bcb
- Unique characters: ‘b’ and ‘c’ => 2 palindromes:
aba
,aca
- For ‘b’: firstIndex = 1, lastIndex = 4, and unique between:
cb
- Unique characters: ‘b’ and ‘c’ => 2 palindromes:
bcb
,bab
![[unique-length-3-palindromic-subsequences-problem-viz.excalidraw]]
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n + 26 * m)
=O(27n)
=O(n)
- Traversing the string to fill
first
andlast
takesO(n)
. - Checking each character in the alphabet takes constant time
O(26)
. - Extracting substrings and counting distinct characters using a set can potentially take
O(m)
for each valid character, wherem
is the length of the substring.
- Traversing the string to fill
- 🧺 Space complexity:
O(n)
- The space complexity is primarily driven by the space required to store the substrings between the first and last occurrences, which can grow up to
O(n)
. Thefirst
andlast
arrays take constant spaceO(1)
as they have a fixed size of 26.
- The space complexity is primarily driven by the space required to store the substrings between the first and last occurrences, which can grow up to