dp
array of length n + 1
, where n
is the length of the string s
. Set dp[0]
to 1, representing the empty subsequence.last
to keep track of the last index where each character was seen.i
from 1 to n
.s[i-1]
, update dp[i]
to 2 * dp[i-1]
.s[i-1]
was seen before (let's say at index j
), subtract dp[j]
from dp[i]
to avoid double counting.last
dictionary with the current index for s[i-1]
.dp[n]
modulo 10^9 + 7
.