3
long getWords(vector<string> &words, string &target, int i, int j) {4
if (j == target.size()) return 1;5
if (i == words[0].size() || words[0].size() - i < target.size() - j) return 0;8
for (int idx = 0; idx < words.size(); idx++) {9
if (words[idx][i] == target[j]) {10
count += getWords(words, target, i + 1, j + 1) % 1000000007;13
count += getWords(words, target, i + 1, j) % 1000000007;14
return count % 1000000007;18
int numWays(vector<string> &words, string target) {19
return getWords(words, target, 0, 0);23
// memoization but still (85/89)25
vector<vector<int>> dp;27
long getWords(vector<string> &words, string &target, int i, int j) {28
if (j == target.size()) return 1;29
if (i == words[0].size() || words[0].size() - i < target.size() - j) return 0;31
if (dp[i][j] != -1) return dp[i][j];34
for (int idx = 0; idx < words.size(); idx++) {35
if (words[idx][i] == target[j]) {36
count += getWords(words, target, i + 1, j + 1) % 1000000007;39
count += getWords(words, target, i + 1, j) % 1000000007;40
return dp[i][j] = count % 1000000007;44
int numWays(vector<string> &words, string target) {45
dp.resize(words[0].size(), vector<int>(target.size(), -1));47
return getWords(words, target, 0, 0);51
// memoization + optimization (by pre calculating frequency)53
vector<vector<int>> dp;54
vector<vector<int>> freq;56
long getWords(vector<string> &words, string &target, int i, int j) {57
if (j == target.size()) return 1;58
if (i == words[0].size() || words[0].size() - i < target.size() - j) return 0;60
if (dp[i][j] != -1) return dp[i][j];63
int curPos = target[j] - 'a';64
count += getWords(words, target, i + 1, j);65
count += freq[i][curPos] * getWords(words, target, i + 1, j + 1);67
return dp[i][j] = count % 1000000007;71
int numWays(vector<string> &words, string target) {72
dp.resize(words[0].size(), vector<int>(target.size(), -1));73
freq.resize(words[0].size(), vector<int>(26, 0));75
for (int i = 0; i < words.size(); i++) {76
for (int j = 0; j < words[0].size(); j++) {77
int curPos = words[i][j] - 'a';81
return getWords(words, target, 0, 0);