LeetCode 730 - Count Different Palindromic Subsequences


LeetCode 664 - Strange Printer
https://www.cnblogs.com/grandyang/p/7942040.html
Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.
A subsequence of a string S is obtained by deleting 0 or more characters from S.
A sequence is palindromic if it is equal to the sequence reversed.
Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.
Example 1:
Input: 
S = 'bccb'
Output: 6
Explanation: 
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.

Example 2:
Input: 
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation: 
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.

Note:
  • The length of S will be in the range [1, 1000].
  • Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.


https://leetcode.com/problems/count-different-palindromic-subsequences/discuss/109507/Java-96ms-DP-Solution-with-Detailed-Explanation
I am not able to pass this question one time but struggle a lot in the basic test cases like "a", "aa", "aaa", "aba", "aabb". Those test cases help my early rough idea to be flawless. The basic idea of DP is easy to understand, I maintain DP[i][j] to record in substring from i to j(included), the number of palindrome without duplicate. Then we consider two cases of the DP equation:
when s.charAt(i) != s.charAt(j):
dp[i][j] = dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1];
When s.charAt(i) == s.charAt(j):
the situation get much more complex and I fix a lot the wrong answers. I have comment the branches where which kind of test cases are considered.
    public int countPalindromicSubsequences(String s) {
        int len = s.length();
        int[][] dp = new int[len][len];

        char[] chs = s.toCharArray();
        for(int i = 0; i < len; i++){
            dp[i][i] = 1;   // Consider the test case "a", "b" "c"...
        }

        for(int distance = 1; distance < len; distance++){
            for(int i = 0; i < len - distance; i++){
                int j = i + distance;
                if(chs[i] == chs[j]){
                    int low = i + 1;
                    int high = j - 1;

              /* Variable low and high here are used to get rid of the duplicate*/

                    while(low <= high && chs[low] != chs[j]){
                        low++;
                    }
                    while(low <= high && chs[high] != chs[j]){
                        high--;
                    }
                    if(low > high){
                        // consider the string from i to j is "a...a" "a...a"... where there is no character 'a' inside the leftmost and rightmost 'a'
                       /* eg:  "aba" while i = 0 and j = 2:  dp[1][1] = 1 records the palindrome{"b"}, 
                         the reason why dp[i + 1][j  - 1] * 2 counted is that we count dp[i + 1][j - 1] one time as {"b"}, 
                         and additional time as {"aba"}. The reason why 2 counted is that we also count {"a", "aa"}. 
                         So totally dp[i][j] record the palindrome: {"a", "b", "aa", "aba"}. 
                         */ 

                        dp[i][j] = dp[i + 1][j - 1] * 2 + 2;  
                    } 
                    else if(low == high){
                        // consider the string from i to j is "a...a...a" where there is only one character 'a' inside the leftmost and rightmost 'a'
                       /* eg:  "aaa" while i = 0 and j = 2: the dp[i + 1][j - 1] records the palindrome {"a"}.  
                         the reason why dp[i + 1][j  - 1] * 2 counted is that we count dp[i + 1][j - 1] one time as {"a"}, 
                         and additional time as {"aaa"}. the reason why 1 counted is that 
                         we also count {"aa"} that the first 'a' come from index i and the second come from index j. So totally dp[i][j] records {"a", "aa", "aaa"}
                        */
                        dp[i][j] = dp[i + 1][j - 1] * 2 + 1;  
                    }
                    else{
                        // consider the string from i to j is "a...a...a... a" where there are at least two character 'a' close to leftmost and rightmost 'a'
                       /* eg: "aacaa" while i = 0 and j = 4: the dp[i + 1][j - 1] records the palindrome {"a",  "c", "aa", "aca"}. 
                          the reason why dp[i + 1][j  - 1] * 2 counted is that we count dp[i + 1][j - 1] one time as {"a",  "c", "aa", "aca"}, 
                          and additional time as {"aaa",  "aca", "aaaa", "aacaa"}.  Now there is duplicate :  {"aca"}, 
                          which is removed by deduce dp[low + 1][high - 1]. So totally dp[i][j] record {"a",  "c", "aa", "aca", "aaa", "aaaa", "aacaa"}
                          */
                        dp[i][j] = dp[i + 1][j - 1] * 2 - dp[low + 1][high - 1]; 
                    }
                }
                else{
                    dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1];  //s.charAt(i) != s.charAt(j)
                }
                dp[i][j] = dp[i][j] < 0 ? dp[i][j] + 1000000007 : dp[i][j] % 1000000007;
            }
        }

        return dp[0][len - 1];
    }
https://leetcode.com/problems/count-different-palindromic-subsequences/discuss/112757/Java-solution-using-simple-DP.-O(n2)-run-time-and-O(n2)-space
For a substring abcba, the extreme characters ('a') can contribute following ways:
  • Step 1: If 'bcb' is pre computed to 3 ('b', 'c', 'bcb') then considering 'a's will make it 6 ('b', 'c', 'bcb', 'aba', 'aca', 'abcba'). This has been taken care by the following code
dp[beg][end] = 2 * dp[beg + 1][end - 1];
  • Step 2: In addition it will also add 'a' and 'aa'. However we need to check if 'a' exists in the current substring. If there is no occurence of 'a'
if(leftIdx > rightIdx){
      dp[beg][end] += 2;
}
  • Step 3: If there is one occurrence of 'a' then we will add just 1 to the count since 'aa' will be duplicated in that case.
else if (leftIdx == rightIdx){
    dp[beg][end] += 1;
}
  • Step 4: Lastly, if there are two occurrences of 'a' then we need to remove the duplicate palindrome added in step 1. For example, 'aabaa' the substring 'aba' will have 'a', 'b', 'aba' palindromes.
    After step 1 we will have 'a', 'b', 'aba', 'aaa', 'aba', 'aabaa'. To remove the additional 'aba' this code has been added
else{
    dp[beg][end] -= dp[leftIdx + 1][rightIdx - 1];
}
The full code :
    private static long MOD = 1000000007;
    public int countPalindromicSubsequences(String S) {
        int len = S.length();
        int[][] dp = new int[len][len];

        // all single letters are a palindrome
        for(int i=0; i<len; i++){
            dp[i][i] = 1;
        }
        
        for(int i=1; i<len; i++){
            // iterating over each substring.
            for(int beg = 0, end = i; end < len; end++, beg++){
                // if the extreme character of current substring match then it contributes to palindrome count
                if(S.charAt(beg) == S.charAt(end)){
                    //eg :  aba, 'b' = 1. Contribution of 'b' in  'abc' are 'b', 'aba' so (contribution of 'b') * 2
                    dp[beg][end] = 2 * dp[beg + 1][end - 1];
                    
                    // idea is to find occurrence of extreme characters with in the substring (excluding the extreme characters)
                    int leftIdx = beg + 1, rightIdx = end - 1;
                    char ch = S.charAt(beg);
                    while(leftIdx <= rightIdx && ch != S.charAt(leftIdx)){
                        leftIdx++;
                    }
                    
                    while(leftIdx <= rightIdx && ch != S.charAt(rightIdx)){
                        rightIdx--;
                    }
                    
                    // if there is no occurrence then we need to add max possible unique palindrome count of 2 characters, which is 2
                    // e,g 'a','a' can form 'a' and 'aa'
                    if(leftIdx > rightIdx){
                        dp[beg][end] += 2;
                    }
                    
                    // if there is one occurrence then we need to add max possible unique palindrome count of 1 character, which is 1
                    else if (leftIdx == rightIdx){
                        dp[beg][end] += 1;
                    }
                    
                    // if there are two occurrence then we need to remove the repetitive palindromes.
                    else{
                        dp[beg][end] -= dp[leftIdx + 1][rightIdx - 1];
                    }
                }
                // else preserve the max count so far. 
                else{
                    dp[beg][end] = dp[beg][end - 1] + dp[beg+1][end] - dp[beg + 1][end - 1];
                }
                
                // this is the modulus logic to prevent overflow.
                dp[beg][end] = (int) ((dp[beg][end] + MOD) % MOD);
            }
        }
        
        return dp[0][len - 1];
    }
X. DFS+Cache
https://leetcode.com/problems/count-different-palindromic-subsequences/discuss/109509/Accepted-Java-Solution-using-memoization
    int div=1000000007;
    public int countPalindromicSubsequences(String S) {    
        TreeSet[] characters = new TreeSet[26];
        int len = S.length();
        
        for (int i = 0; i < 26; i++) characters[i] = new TreeSet<Integer>();
        
        for (int i = 0; i < len; ++i) {
            int c = S.charAt(i) - 'a';
            characters[c].add(i);
        }
        Integer[][] dp = new Integer[len+1][len+1];
         return memo(S,characters,dp, 0, len);
    }
    
    public int memo(String S,TreeSet<Integer>[] characters,Integer[][] dp,int start,int end){
        if (start >= end) return 0;
        if(dp[start][end]!=null) return dp[start][end];
       
            long ans = 0;
            
            for(int i = 0; i < 26; i++) {
                Integer new_start = characters[i].ceiling(start);
                Integer new_end = characters[i].lower(end);
              if (new_start == null || new_start >= end) continue;
                 ans++;
                if (new_start != new_end) ans++;
                ans+= memo(S,characters,dp,new_start+1,new_end);
                
            }
            dp[start][end] = (int)(ans%div);
            return dp[start][end];
    }


这道题给了给了我们一个字符串,让我们求出所有的非空回文子序列的个数,虽然这题限制了字符只有四种,但是我们还是按一般的情况来解吧,可以有26个字母。然后说最终结果要对一个很大的数字取余,这就暗示了结果会是一个很大的值,那么对于这种问题一般都是用DP或者是带记忆数组memo的递归来解,二者的本质其实是一样的。我们先来看带记忆数组memo的递归解法,这种解法的思路是一层一层剥洋葱,比如"bccb",按照字母来剥,先剥字母b,确定最外层"b _ _ b",这会产生两个回文子序列"b"和"bb",然后递归进中间的部分,把中间的回文子序列个数算出来加到结果res中,然后开始剥字母c,找到最外层"cc",此时会产生两个回文子序列"c"和"cc",然后由于中间没有字符串了,所以递归返回0,按照这种方法就可以算出所有的回文子序列了。
我们建立一个二维数组chars,外层长度为26,里面放一个空数组。这是为了统计每个字母在原字符串中出现的位置,然后定义一个二维记忆数组memo,其中memo[i][j]表示第i个字符到第j个字符之间的子字符串中的回文子序列的个数,初始化均为0。然后我们遍历字符串S,将每个字符的位置加入其对应的数组中,比如对于"bccb",那么有:
b -> {0, 3}
c -> {1, 2}
然后在[0, n]的范围内调用递归函数,在递归函数中,首先判断如果start大于等于end,返回0。如果当前位置在memo的值大于0,说明当前情况已经计算过了,直接返回memo数组中的值。否则进行所有字母的遍历,如果某个字母对应的数组中没有值,说明该字母不曾在字符串中出现,跳过。然后我们在字母数组中查找第一个不小于start的位置,查找第一个小于end的位置,当前循环中,start为0,end为4,当前处理字母b,我们的new_start指向0,new_end指向3,如果当前new_start指向了end(),或者其指向的位置大于end,说明当前范围内没有字母b,直接跳过,否则结果res自增1,因为此时new_start存在,至少有个单个的字母b,也可以当作回文子序列,然后看new_start和new_end如果不相同,说明两者各指向了不同的b,此时res应自增1,因为又增加了一个新的回文子序列"bb",下面就是对中间部分调用递归函数了,把返回值加到结果res中。此时字母b就处理完了,现在处理字母c,此时的start还是0,end还是4,new_start指向1,new_end指向2,跟上面的分析相同,new_start在范围内,结果自增1,因为加上了"c",然后new_start和new_end不同,结果res再自增1,因为加上了"cc",其中间没有字符了,调用递归的结果是0,for循环结束,我们将memo[start][end]的值对超大数取余,将该值返回即可,



我们再来看一种迭代的写法,使用一个二维的dp数组,其中dp[i][j]表示子字符串[i, j]中的不同回文子序列的个数,我们初始化dp[i][i]为1,因为任意一个单个字符就是一个回文子序列,其余均为0。这里的更新顺序不是正向,也不是逆向,而是斜着更新,对于"bccb"的例子,其最终dp数组如下,我们可以看到其更新顺序分别是红-绿-蓝-橙。
  b c c b
b 1 2 3 6
c 0 1 2 3
c 0 0 1 2
b 0 0 0 1
这样更新的好处是,更新当前位置时,其左,下,和左下位置的dp值均已存在,而当前位置的dp值需要用到这三个位置的dp值。我们观察上面的dp数组,可以发现当S[i]不等于S[j]的时候,dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1],即当前的dp值等于左边值加下边值减去左下值,因为算左边值的时候包括了左下的所有情况,而算下边值的时候也包括了左下值的所有情况,那么左下值就多算了一遍,所以要减去。而当S[i]等于S[j]的时候,情况就比较复杂了,需要分情况讨论,因为我们不知道中间还有几个和S[i]相等的值。举个简单的例子,比如"aba"和"aaa",当i = 0, j = 2的时候,两个字符串均有S[i] == S[j],此时二者都新增两个子序列"a"和"aa",但是"aba"中间的"b"就可以加到结果res中,而"aaa"中的"a"就不能加了,因为和外层的单独"a"重复了。我们的目标就要找到中间重复的"a"。所以我们让left = i + 1, right = j - 1,然后对left进行while循环,如果left <= right, 且S[left] != S[i]的时候,left向右移动一个;同理,对right进行while循环,如果left <= right, 且S[right] != S[i]的时候,left向左移动一个。这样最终left和right值就有三种情况:
1. 当left > righ时,说明中间没有和S[i]相同的字母了,就是"aba"这种情况,那么就有dp[i][j] = dp[i + 1][j - 1] * 2 + 2,其中dp[i + 1][j - 1]是中间部分的回文子序列个数,为啥要乘2呢,因为中间的所有子序列可以单独存在,也可以再外面包裹上字母a,所以是成对出现的,要乘2。加2的原因是外层的"a"和"aa"也要统计上。
2. 当left = right时,说明中间只有一个和S[i]相同的字母,就是"aaa"这种情况,那么有dp[i][j] = dp[i + 1][j - 1] * 2 + 1,其中乘2的部分跟上面的原因相同,加1的原因是单个字母"a"的情况已经在中间部分算过了,外层就只能再加上个"aa"了。
3. 当left < right时,说明中间至少有两个和S[i]相同的字母,就是"aabaa"这种情况,那么有dp[i][j] = dp[i + 1][j - 1] * 2 - dp[left + 1][right - 1],其中乘2的部分跟上面的原因相同,要减去left和right中间部分的子序列个数的原因是其被计算了两遍,要将多余的减掉


讨论:这道题确实是一道很难的题,和它类似的题目还有几道,虽然那些题有的还有非DP解法,但是DP解法始终是核心的,也是我们最应该掌握的方法。首先我们要分清子串和子序列的题,个人感觉子序列要更难一些。在之前那道Longest Palindromic Subsequence中要我们求最长的回文子序列,我们需要逆向遍历dp数组,当s[i]和s[j]相同时,长度为中间部分的dp值加2,否则就是左边值和下边值中的较大值,因为是子序列,不匹配就可以忽略当前字符。而对于回文子串的问题,比如Longest Palindromic SubstringPalindromic Substrings,一个是求最长的回文子串,一个是求所有的回文子串个数,他们的dp定义是看子串[i, j]是否是回文串,求最长回文子串就是维护一个最大值,不停用当前回文子串的长度更新这个最大值,同时更新最大值的左右边界。而求所有回文子串的个数就是如果当前dp[i][j]判断是回文串,计数器就自增1。而判断当前dp[i][j]是否是回文串的核心就是s[i]==s[j],且i,j中间没有字符了,或者中间的dp值为true。

X. Videos
花花酱 LeetCode 730. Count Different Palindromic Subsequences - 刷题找工作 EP114

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts