Ways of transforming one string to other by removing 0 or more characters
Given two sequences A, B, find out number of unique ways in sequence A, to form a subsequence of A that is identical to the sequence B. Transformation is meant by converting string A (by removing 0 or more characters) to string B.
Examples:
Input : A = “abcccdf”, B = “abccdf”
Output : 3
Explanation : Three ways will be -> “ab.ccdf”,
“abc.cdf” & “abcc.df” .
“.” is where character is removed.
Input : A = “aabba”, B = “ab”
Output : 4
Expalnation : Four ways will be -> “a.b…”,
“a…b.”, “.ab…” & “.a.b.” .
“.” is where characters are removed.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int nums_delete(string a, string b) {
int n = a.length();
int m = b.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= n; ++i) {
dp[i][0] = 1;
}
int result = 0;
for (int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if (a[i - 1] == b[j - 1]) {
dp[i][j] += dp[i - 1][j - 1];
}
dp[i][j] += dp[i - 1][j];
}
}
return dp[n][m];
}
int main() {
cout << nums_delete("aabba", "ab") << endl;
cout << nums_delete("abcccdf", "abccdf") << endl;
return 0;
}