魔法师 (@Constanline)Leetcode每日一题 —— 940. 不同的子序列 II 中发帖

思路
依然DP,思路比较朴素晚点优化。 
设dp[i][j]为使用字符串长度i,子序列长度j的可能性,cnt[j][x]为子序列第j位使用第x个字符的重复值(像 a ba 与 ab a就会重复)。 
那么选 不选当前位,结果为 dp[i - 1][j],选当前位,结果为 dp[i - 1][j - 1] - cnt[j][chr]。 
而对于重复值,后面的第x位同字符选中的可能性包含了前面所有第x位同字符选中的可能性。 
代码
class Solution {
    private static final int MOD = 1000000007;
    public int distinctSubseqII(String s) {
        int n = s.length();
        int[][] dp = new int[n + 1][n + 1];
  ...
 
 
Back to Top