[Swift]LeetCode115. 不同的子序列 | Distinct Subsequences

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

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).

Example 1:

Input: S = "rabbbit", T = "rabbit"
Output: 3
Explanation:

As shown below, there are 3 ways you can generate "rabbit" from S.
(The caret symbol ^ means the chosen letters)

rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"
Output: 5
Explanation:

As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)

babgbag
^^ ^
babgbag
^^    ^
babgbag
^    ^^
babgbag
  ^  ^^
babgbag
    ^^^

给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数。

一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)

示例 1:

输入: S = "rabbbit", T = "rabbit"
输出: 3
解释:

如下图所示, 有 3 种可以从 S 中得到 "rabbit" 的方案。
(上箭头符号 ^ 表示选取的字母)

rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

示例 2:

输入: S = "babgbag", T = "bag"
输出: 5
解释:

如下图所示, 有 5 种可以从 S 中得到 "bag" 的方案。 
(上箭头符号 ^ 表示选取的字母)

babgbag
^^ ^
babgbag
^^    ^
babgbag
^    ^^
babgbag
  ^  ^^
babgbag
    ^^^

超过时间限制
 1 class Solution {
 2     func numDistinct(_ s: String, _ t: String) -> Int {
 3         if s.isEmpty {return 0}
 4         let len:Int = s.count
 5         var dp:[Int] = [Int](repeating:1,count:len)
 6         var pre:Int,temp:Int
 7         for i in 0..<t.count
 8         {
 9             pre = dp[0]
10             dp[0] = (i == 0 && s[0] == t[0]) ? 1 : 0
11             for k in 1..<len
12             {
13                 temp = dp[k]
14                 dp[k] = dp[k - 1] + (s[k] == t[i] ? pre : 0)
15                 pre = temp
16             }
17         }
18         return dp[len - 1]
19     }
20 }
21 
22 extension String {
23     //subscript函数可以检索数组中的值
24     //直接按照索引方式截取指定索引的字符
25     subscript (_ i: Int) -> Character {
26         //读取字符
27         get {return self[index(startIndex, offsetBy: i)]}
28     }
29 }

超过时间限制

 1 class Solution {
 2     func numDistinct(_ s: String, _ t: String) -> Int {
 3         if s.isEmpty || t.isEmpty {return 0}
 4         //字符串t的长度
 5         var len1:Int = t.count
 6         //字符串s的长度
 7         var len2:Int = s.count
 8         //dp[i][j]表示从t的从0开始为i的子串和s的从0开始为j的子串的匹配个数。
 9         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:len2 + 1),count:len1 + 1)
10         
11         //首先i对于的就是字符串t里面的索引
12         for i in 0..<(len2+1)
13         {
14             dp[0][i]=1
15         }
16         
17         //首先从第二行开始计算,第一行中第一个数总是为1的。因为两个空字符串是匹配的
18         for i in 1..<(len1+1)
19         {
20             dp[i][0]=0
21         }
22         
23         //动态规划
24         for i in 1...len1
25         {
26              //首先j对于的就是字符串s里面的索引。(s>t)
27             for j in 1...len2
28             {
29                 if t.charAt(i-1) == s.charAt(j-1)
30                 {
31                    dp[i][j] = dp[i-1][j-1]+dp[i][j-1]
32                 }
33                 else
34                 {
35                     dp[i][j] = dp[i][j-1]
36                 }
37             }
38         }
39         return dp[len1][len2]
40     }
41 }
42 
43 extension String {
44     //获取指定索引位置的字符,返回为字符串形式
45     func charAt(_ num:Int) -> Character
46     {
47         return self[index(self.startIndex,offsetBy: num)]
48     }
49 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/9952756.html