charOccurrences
with zeros.col
and each word in words
to fill charOccurrences
with the count of each character.dp
with zeros.dp[0][0] = 1
as the base case.length
from 0
to targetLength
, and for each col
from 0
to wordLength - 1
, do the following:
length < targetLength
, update dp[length + 1][col + 1]
by adding charOccurrences[target[length] - 'a'][col] * dp[length][col]
.dp[length][col + 1]
by adding dp[length][col]
.dp[targetLength][wordLength]
as the result.