【LeetCode】Distinct Subsequences 解题报告

【题目】

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is
a subsequence of "ABCDE" while "AEC" is
not).

Here is an example:

S = "rabbbit", T = "rabbit"

Return 3.

【解析】

题意:给定两个字符串S和T,返回S中有多少个子序列和T一样。

思路:动态规划。本来想用一个一维数组来存储结果,花好久时间没弄出来,还是用二维数组写起来更清楚容易。直接看代码吧,注释应该还算比较清楚。

public class Solution {
    public int numDistinct(String S, String T) {
        if (S == null || T == null) return 0;
        if (S.equals("") || T.equals("")) return 0;

        //table[i][j]表示S[0~i]中有多少个T[0~j]的个数
        int[][] table = new int[S.length()][T.length()];

        //给table第一行赋值,意思是S[0]中有多少个T[0~j]
        //显然,S[0]最多包含一个T[0],故table[0][1~n]都为0
        if (S.charAt(0) == T.charAt(0)) {
            table[0][0] = 1;
        }

        for (int i = 1; i < S.length(); i++) {
            char s = S.charAt(i);

            //给table[i][0]赋值,意思是S[0~i]中有多少个T[0]
            if (s == T.charAt(0)) {
                table[i][0] = table[i - 1][0] + 1;
            } else {
                table[i][0] = table[i - 1][0];
            }

            for (int j = 1; j < T.length(); j++) {
                char t = T.charAt(j);
                if (s == t) {
                    //如果用S[i]匹配T[j],那么S[0~i-1]中共有table[i-1][j-1]个T[0~j-1]
                    //如果不用S[i]匹配T[i],那么S[0~i]中共有table[i-1][j]个T[0~j]
                    table[i][j] = table[i - 1][j - 1] + table[i - 1][j];
                } else {
                    //S[i]!=T[j]时,S[0~i]中共有table[i-1][j]个T[0~j]
                    table[i][j] = table[i - 1][j];
                }
            }

        }

        return table[S.length() - 1][T.length() - 1];
    }
}
时间: 2024-10-09 22:24:54

【LeetCode】Distinct Subsequences 解题报告的相关文章

LeetCode: Distinct Subsequences 解题报告

Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without di

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from

[LeetCode] Distinct Subsequences 解题思路

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

Distinct Subsequences 解题报告

题目:给两个字符串S和T,判断T在S中出现的次数. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

LeetCode: Distinct Subsequences

LeetCode: Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters

LeetCode: Distinct Subsequences [115]

[题目] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the rela

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.