# Count Construct with Tabulation

Updated: 01 February 2024

# Problem

The same problem as the Count Construct Memoization Problem but implemented using the tabular style in the previous problems

In this case, we will store an index for each letter and the value of how many substrings can start from that position - if it is possible then we will go to the end of ths substring and mark that position as false

# Tabulation Implementation

The implementation of this can be seen below:

`dynamic-programming/tabulation/count-construct.ts`

```
export const countConstruct = (target: string, subs: string[]): number => {
const table = new Array<number>(target.length + 1).fill(0);
table[0] = 1;
for (let index = 0; index <= target.length; index++) {
if (table[index]) {
for (const sub of subs) {
const fromIndex = target.slice(index);
if (fromIndex.startsWith(sub)) {
const nextIndex = index + sub.length;
table[nextIndex] += table[index];
}
}
}
}
return table[target.length];
};
```

In the above, the time complexity $O(m^2 *n)$ and space complexity $O(m)$

The reason we say $m^2$ and not $m$ in the time complexity is because of the array copy operation that we do to assign the latest value