Count Number of Special Subsequences LeetCode solution in Java

1
class Solution {
2
public int countSpecialSubsequences(int[] nums) {
3
long z = 0; // starting and ending with zero
4
long zo = 0; // starting with zero and ending with One
5
long zot = 0; // starting with zero and ending Two
6
int mod = 1000000007;
7
for (int i : nums) {
8
if (i == 0) {
9
z = ((2 * z) % mod + 1) % mod; // zero = 2*zero+1
10
} else if (i == 1) {
11
zo = ((2 * zo) % mod + z % mod) % mod; // zero = 2*zeroOne+zero
12
} else {
13
zot = ((2 * zot) % mod + zo % mod) % mod; // zeroOneTwo = 2*zeroOneTwo+zeroOne
14
}
15
}
16
return (int) (zot % mod);
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0