Skip to content

Practice Count Number of Special Subsequences

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1955
LeetCode Problem

Count Number of Special Subsequences

Problem Statement

A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s. Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 10^9 + 7. A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are different if the set of indices chosen are different.

Source: Count Number of Special Subsequences on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [0,1,2,2]
Output
3

# Example 2

Input
nums = [2,2,0,0]
Output
0

# Example 3

Input
nums = [0,1,2,0,1,2]
Output
7

Constraints

1 <= nums.length <= 10^5
0 <= nums[i] <= 2

Related LeetCode Problems

Practice more problems similar to Count Number of Special Subsequences.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.