Similar Problems
Similar Problems not available
Count Number Of Special Subsequences - Leetcode Solution
Companies:
LeetCode: Count Number Of Special Subsequences Leetcode Solution
Difficulty: Hard
Topics: dynamic-programming array
The problem “Count Number Of Special Subsequences” can be found on LeetCode.
Problem Statement:
Given a string s containing only three types of characters: '0', '1', and '2'. Return the number of subsequences of s that are special. A subsequence is considered special if it has the following properties:
- It is non-empty.
- It contains only one type of character.
- For each different type of character, there is one subsequence that contains only that character and has a length of at least two.
Note:
- A subsequence of a string s is obtained by deleting zero or more characters from s.
- A sequence of length n is called a subsequence of a string if it can be obtained from the string by deleting some (or none) characters without changing the order of the remaining characters.
Solution:
To solve this problem, we can use a dynamic programming approach. Specifically, we can define two arrays:
- count: An array where count[i][j] represents the number of subsequences of the substring s[0:i] that ends with character j (0, 1 or 2).
- sum: An array where sum[i][j] represents the number of special subsequences of the substring s[0:i] that end with character j (0, 1 or 2).
To calculate count[i][j], we can use the following recurrence relation:
- If s[i] is not equal to j, then count[i][j] = count[i-1][j].
- Otherwise, count[i][j] = count[i-1][j] + 1.
To calculate sum[i][j], we can use the following recurrence relation:
- If s[i] is not equal to j, then sum[i][j] = sum[i-1][j].
- Otherwise, sum[i][j] = sum[i-1][j] + count[i-1][j] + 1.
The base cases are:
- count[0][0] = 1 (because an empty string has one subsequence).
- count[0][1] = count[0][2] = 0 (because an empty string cannot end with character 1 or 2).
- sum[0][0] = sum[0][1] = sum[0][2] = 0 (because an empty string has no special subsequences).
Finally, the answer is given by sum[n-1][0] + sum[n-1][1] + sum[n-1][2], where n is the length of the string s.
Time Complexity:
The time complexity of the above approach is O(n), where n is the length of the string s.
Space Complexity:
The space complexity of the above approach is also O(n), as we are using two arrays of size n.
Implementation:
Here is the Python3 code implementation of the above approach:
def countSpecialSubsequences(s: str) -> int: MOD = 10**9 + 7 n = len(s) count = [[0] * 3 for _ in range(n)] sum = [[0] * 3 for _ in range(n)] count[0][int(s[0])] = 1 for i in range(1, n): for j in range(3): if j != int(s[i]): count[i][j] = count[i-1][j] else: count[i][j] = (count[i-1][j] + 1) % MOD for k in range(3): if k != j: count[i][j] = (count[i][j] + count[i-1][k]) % MOD sum[i][j] = (sum[i-1][j] + count[i-1][j] + 1) % MOD for k in range(3): if k != j: sum[i][j] = (sum[i][j] + sum[i-1][k]) % MOD return (sum[n-1][0] + sum[n-1][1] + sum[n-1][2]) % MOD
We can test our solution for some sample inputs:
Input: s = "2020" Output: 4 Explanation: The special subsequences are: "2", "2", "0", "20".
Input: s = "111111" Output: 0 Explanation: There are no special subsequences.
Input: s = "10101" Output: 2 Explanation: The special subsequences are: "1", "101".
Conclusion:
In this article, we have discussed the “Count Number Of Special Subsequences” problem and have provided a dynamic programming-based solution to it. This problem is an interesting variation of the classic dynamic programming implementation and can also be used to improve your dynamic programming skills.
Count Number Of Special Subsequences Solution Code
1