1. 程式人生 > >[Swift]LeetCode730. 統計不同回文子字符串 | Count Different Palindromic Subsequences

[Swift]LeetCode730. 統計不同回文子字符串 | Count Different Palindromic Subsequences

ddc leetcode which ted del func example 回文 bcd

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‘}
    .

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

通過從 S 中刪除 0 個或多個字符來獲得子字符序列。

如果一個字符序列與它反轉後的字符序列一致,那麽它是回文字符序列。

如果對於某個 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 }

[Swift]LeetCode730. 統計不同回文子字符串 | Count Different Palindromic Subsequences