Count Palindromic Subsequences - Complete Solution Guide
Count Palindromic Subsequences is LeetCode problem 2484, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string of digits s , return the number of palindromic subsequences of s having length 5 . Since the answer may be very large, return it modulo 10 9 + 7 . Note: A string is palindromic if it reads the same forward and backward. A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. Example 1: Input: s = "103301" Output: 2 Explanation: There are 6 possible subsequences of length 5: "10330",
Detailed Explanation
The problem asks us to find the number of palindromic subsequences of length 5 in a given string 's' consisting of digits. A subsequence is a string formed by deleting some characters from 's' without changing the order of the remaining characters. A palindrome is a string that reads the same forwards and backward. The answer must be returned modulo 10^9 + 7 because the count of palindromic subsequences can be very large. For example, in the string "103301", the subsequence "10301" is a palindrome, and we need to count how many such palindromes of length 5 exist.
Solution Approach
The solution uses dynamic programming to efficiently count the palindromic subsequences of length 5. It precomputes the counts of digit pairs in the suffix of the string and stores them in `suffix_pairs`. It also maintains counts of digits in the prefix of the string in `prefix_counts` and the counts of digit pairs in the prefix `prefix_pairs`. Then the code iterates through the string and, at each index `k`, considers that the digit at `k` could be the middle digit 'C' of the palindrome. It uses the `prefix_pairs` to find pairs of digits 'AB' before index `k`, and uses `suffix_pairs` to find matching pairs of digits 'BA' after index `k`. It then accumulates the counts of these combinations.
Step-by-Step Algorithm
- Step 1: Initialize variables: `n` (length of string), `MOD` (10^9 + 7), `digits` (array of integer representation of digits in string), `suffix_pairs` (3D array to store the counts of digit pairs in the suffixes), `suffix_counts` (array to store the counts of digits in the suffixes).
- Step 2: Calculate `suffix_pairs` and `suffix_counts`. Iterate from the end of the string to the beginning. For each digit `c` at index `k`, increment the count of `suffix_pairs[k][c][d2]` by `suffix_counts[d2]` for all digits `d2`. This counts the number of times a digit `c` is followed by a digit `d2` in the suffix. Update suffix_counts[c].
- Step 3: Initialize variables: `total` (to store the total count of palindromes), `prefix_counts` (array to store the counts of digits in the prefixes), and `prefix_pairs` (2D array to store the counts of digit pairs in the prefixes).
- Step 4: Iterate through the string from the beginning to the end. At each index `k`, the digit `c` is considered as the middle digit of the palindrome. Calculate `current_suffix_pairs` which stores suffix pairs after index k. Iterate through all possible digit pairs `x` and `y` (0-9). Update `total` by adding `prefix_pairs[x][y] * current_suffix_pairs[y][x]` to the total. This finds the number of palindromes ABCBA where C is the digit at index k.
- Step 5: Update `prefix_pairs` and `prefix_counts`. Increment `prefix_pairs[d1][c]` by `prefix_counts[d1]` for all digits `d1`, where `c` is the digit at index `k`. Then increment `prefix_counts[c]` to count the occurrence of `c` in prefix.
- Step 6: Return `total` modulo `MOD`.
Key Insights
- Insight 1: Since we are looking for palindromes of length 5, they must have the form ABCBA, where A, B, and C can be any digit.
- Insight 2: Dynamic programming is a good approach since we want to count the number of ways to build a palindrome, and can break the problem down into prefix and suffix counts.
- Insight 3: We need to precompute the counts of digit pairs in the suffix to efficiently find matching digit pairs to form the palindromes. We also need to calculate prefix counts.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Dynamic Programming.
Companies
Asked at: Citadel, Goldman Sachs, Salesforce.