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.
"ace" is a subsequence of "abcde".Example 1:
Input: s = "aabca" Output: 3 Explanation: The 3 palindromic subsequences of length 3 are: - "aba" (subsequence of "aabca") - "aaa" (subsequence of "aabca") - "aca" (subsequence of "aabca")
Example 2:
Input: s = "adc" Output: 0 Explanation: There are no palindromic subsequences of length 3 in "adc".
Example 3:
Input: s = "bbcbaba" Output: 4 Explanation: The 4 palindromic subsequences of length 3 are: - "bbb" (subsequence of "bbcbaba") - "bcb" (subsequence of "bbcbaba") - "bab" (subsequence of "bbcbaba") - "aba" (subsequence of "bbcbaba")
Constraints:
3 <= s.length <= 105s consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method in this scenario is like trying every possible combination of three letters from a given text to see if any form a palindrome. We check each group of three for the palindrome property, without any shortcuts or optimizations. We simply enumerate all possibilities.
Here's how the algorithm would work step-by-step:
def unique_length_3_palindromic_subsequences_brute_force(text):
unique_palindromes = set()
text_length = len(text)
for first_index in range(text_length):
for second_index in range(first_index + 1, text_length):
for third_index in range(second_index + 1, text_length):
# Check if the first and third letters match to form a palindrome.
if text[first_index] == text[third_index]:
palindrome = text[first_index] + text[second_index] + text[third_index]
unique_palindromes.add(palindrome)
return len(unique_palindromes)To find the count of unique length-3 palindromes efficiently, we focus on identifying the first and last appearances of each character. Then, for each pair of identical characters found, we count the unique characters appearing between them. This avoids unnecessary checks and ensures we only count unique palindromes.
Here's how the algorithm would work step-by-step:
def unique_palindromic_subsequences(input_string):
first_occurrence = {}
last_occurrence = {}
string_length = len(input_string)
for index in range(string_length):
character = input_string[index]
if character not in first_occurrence:
first_occurrence[character] = index
last_occurrence[character] = index
count_of_palindromes = 0
for character in 'abcdefghijklmnopqrstuvwxyz':
if character in first_occurrence:
first_index = first_occurrence[character]
last_index = last_occurrence[character]
# Skip if the character appears only once.
if first_index < last_index:
# Find unique characters between first and last occurences.
characters_in_between = set()
for index in range(first_index + 1, last_index):
characters_in_between.add(input_string[index])
# Increment the count by the number of unique middle chars.
count_of_palindromes += len(characters_in_between)
return count_of_palindromes| Case | How to Handle |
|---|---|
| Empty string | Return 0 because an empty string contains no subsequences. |
| String with length less than 3 | Return 0 because a string shorter than 3 characters cannot contain a length-3 subsequence. |
| String with length equal to 3 which is a palindrome | Return 1 if the string is a palindrome, otherwise return 0. |
| String with all same characters e.g., 'aaaa' | Count of unique palindromic subsequences is 1, consisting of the repeated character. |
| String with distinct characters e.g., 'abc' | Count of unique palindromic subsequences is 0. |
| Long string with many repeated characters | Ensure solution uses efficient data structures like sets or dictionaries to avoid quadratic complexity in subsequence counting. |
| String containing only one character type that form palindromes | The solution should only count the single palindromic subsequence formed by that character once, preventing overcounting. |
| String consisting of almost the same characters but with tiny change | The solution should correctly identify the existing unique palindromes regardless of minor character changes. |