[Swift]LeetCode730. 統計不一樣迴文子字符串 | Count Different Palindromic Subsequences

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-dnxmleaw-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.git

A subsequence of a string S is obtained by deleting 0 or more characters from S.github

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

Example 1:code

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:htm

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

Note:blog

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

給定一個字符串 S,找出 S 中不一樣的非空迴文子序列個數,並返回該數字與 10^9 + 7 的模。字符串

經過從 S 中刪除 0 個或多個字符來得到子字符序列。get

若是一個字符序列與它反轉後的字符序列一致,那麼它是迴文字符序列。

若是對於某個  iA_i != B_i,那麼 A_1, A_2, ... 和 B_1, B_2, ... 這兩個字符序列是不一樣的。 

示例 1:

輸入:
S = 'bccb'
輸出:6
解釋:
6 個不一樣的非空迴文子字符序列分別爲:'b', 'c', 'bb', 'cc', 'bcb', 'bccb'。
注意:'bcb' 雖然出現兩次但僅計數一次。

示例 2:

輸入:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
輸出:104860361
解釋:
共有 3104860382 個不一樣的非空迴文子字符序列,對 10^9 + 7 取模爲 104860361。 

提示:

  • 字符串 S 的長度將在[1, 1000]範圍內。
  • 每一個字符 S[i] 將會是集合 {'a', 'b', 'c', 'd'} 中的某一個。

Runtime: 760 ms
Memory Usage: 25.9 MB
 1 class Solution {
 2     func countPalindromicSubsequences(_ S: String) -> Int {
 3         var arr:[Character] = Array(S)
 4         var n:Int = S.count
 5         var M:Int = Int(1e9 + 7)
 6         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n),count:n)
 7         for i in 0..<n
 8         {
 9             dp[i][i] = 1
10         }
11         for len in 1..<n
12         {
13             for i in 0..<(n - len)
14             {
15                 var j:Int = i + len
16                 if arr[i] == arr[j]
17                 {
18                     var left:Int = i + 1
19                     var right:Int = j - 1
20                     while (left <= right && arr[left] != arr[i])
21                     {
22                         left += 1
23                     }
24                     while (left <= right && arr[right] != arr[i])
25                     {
26                         right -= 1
27                     }
28                     if left > right
29                     {
30                         dp[i][j] = dp[i + 1][j - 1] * 2 + 2
31                     }
32                     else if left == right
33                     {
34                         dp[i][j] = dp[i + 1][j - 1] * 2 + 1
35                     }
36                     else
37                     {
38                         dp[i][j] = dp[i + 1][j - 1] * 2 - dp[left + 1][right - 1]
39                     }
40                 }
41                 else
42                 {
43                     dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1]
44                 }
45                 dp[i][j] = (dp[i][j] < 0) ? dp[i][j] + M : dp[i][j] % M
46             }
47         }
48         return dp[0][n - 1]
49     }
50 }
相關文章
相關標籤/搜索